...

View Full Version : Template Class Question



kspecks
01-30-2012, 02:04 AM
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.

oracleguy
02-01-2012, 07:33 PM
Perhaps you could mention what the specific issue is? Does it not compile? Or does it not run correctly?

kspecks
02-02-2012, 02:12 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum