Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Template Class Question

    I wrote a basic template class and can not figure out what is wrong with it. I went to school for video game development and I am just trying to brush up on basic skills any help would be very appreciated here is the code.

    template<typename T>
    class Array
    {
    public:
    Array(unsigned arraySize):
    data(0), size(arraySize)
    {
    if(size > 0)
    data = new T[size];
    }

    ~Array()
    {
    if(data) delete[] data;
    }

    void setValue(unsigned index, const T& value)
    {
    if(index < size)
    data[index] = value;
    }

    T getValue(unsigned index) const
    {
    if(index < size)
    return data[index];
    else
    return T();
    }

    private:
    T* data;
    unsigned size;
    };

    I know there is something in here that needs fixed but I can't see it and maybe some fresh eyes could help.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Perhaps you could mention what the specific issue is? Does it not compile? Or does it not run correctly?
    OracleGuy

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I guess a more general question would be what is wrong with the implementation of this class I know there is a problem I just can't see it. My best guess is no error checking but it's been a while since I coded C++. I don't know if returning T and not NULL would cause a problem in the getValue function or not. When I did a general test in VS I couldn't find any problems.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •