Template storage container

Hello all,

Can someone explain the following code for me line by line please? In particular, im having dificulty understanding the addElement function..Thanks
template<class T, int size>
class Storage
{
T values[size];
int next;
public:
T& operator [](int index)
{
return values[index];
}
bool addElement(T value)
{
if (next>size) return false;
values[next++]=value;
return true;
}
int getSize() { return next-1; }
};

Open in new window

oggiemcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subrat (C++ windows/Linux)Software EngineerCommented:
This is a class template.
>>if (next>size) return false;
here whts value of next????
next isn't initialized.So it's a bad programming....

If want to know about class template follow:http://www.cplusplus.com/doc/tutorial/templates/

and tell us if u want any help.
Infinity08Commented:
This defines a container class called Storage that can store a fixed number of items (given by the template parameter 'size') of a given type (given by the template parameter 'T').

The data members of the class are an array of 'size' objects of type 'T' called 'values', and an int 'next' that holds the index of the next free position in the array (ie. one after the last element in the array).

A few member functions are defined :

(a) the index operator (operator[]) returns the object at a given index.

(b) the 'addElement' member function adds a new object to the end of the array. It checks whether there is still room in the array, and if so adds the given object to the array, and increments the 'next' index. It returns true or false, depending on whether the insertion succeeded or not.

(c) the 'getSize' member function returns the current amount of items in the array.


That said, there are a few issues with this code that'll make it misbehave :

(a) the 'next' data member is never initialized. There should be a constructor in the class that initializes it to 0.

(b) the operator[] never checks whether the index is out of bounds or not (either lower than 0, or larger than or equal to 'next').

(c) the 'addElement' member function attempts to do a check on the index, but doesn't do it correctly. It should check whether the given index is lower than 0, or larger than or equal to 'next'.

(d) the 'getSize' member function should return 'next', not 'next - 1'. Because array indexing starts at 0.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oggiemcAuthor Commented:
thanks for reply infinity,

>>(a) the 'next' data member is never initialized. There should be a constructor in the class that initializes it to 0.
Can i not just do "int next = 0;" ?

>>(b) the operator[] never checks whether the index is out of bounds or not (either lower than 0, or larger than or equal to 'next').
i thought 'next' was the index?:
>>and an int 'next' that holds the index of the next free position in the array (ie. one after the last element in the array).



if (next>size) return false;
values[next++]=value;
return true;

 
would this do the same job? :

if (next>size) {return false;}
else {values[next++]=value;
return true;}

Thanks







Infinity08Commented:
>> Can i not just do "int next = 0;" ?

No. That doesn't work for class data members. A constructor is what's used to initialize data members.


>> i thought 'next' was the index?:

'next' just indicates the next free position. The index used for the subscript operator (operator[]) is passed in as parameter (called 'index' unsurprisingly).


>> would this do the same job? :

Yes it would. But it would also have the same problems I pointed out.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.