Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
My assignment is to create a class that implements a safe resizable array, which can contain data of any single type.

Use templates so that the data stored in the arrays can be of any type
Provide a "default" (no parameter constructor that sets the array to some reasonable default initial size
Provide another constructor with one int parameter that allows the programmer to specify the original size
So these would all be possible declarations (just an example)
basically what I need to know if I am on the right track, and if not what should I do.

C++
array<int> A;
array<string> B,C;
array<double> D(100);

template<typename stuff> //my template
class safearray
{
public:
    stuff data[];
    safearray def_size() // default size
        {int size = 10; return size;}

    //constructor which the user can later use to set  the array's size themselves
    safearray new_size(new_int) 
        {data = new int [newsize]; size = newsize; }
Posted
Updated 29-Jan-15 21:34pm
v4

Yes, it looks you are on the right track. However, you know, constructors should have the same name of the class.
 
Share this answer
 
As CPallini said, you are almost on the right track. There are just a couple of small issues that need to be corrected:

You declared your internal array as
C++
stuff data[];

And that doesn't work here. In C++ there is no such thing as a variable size array. Instead, you should use a pointer to your allocated space, for example:
C++
stuff* pData;
...
safearray (int newSize)
    {pData = new int[newSize];}


Then there is an issue in naming conventions. You call the cell type stuff, which is kind of misleading. stuff would be a good name for a variable that holds your data, but not for the data type itself. For example stuffType would convey that meaning. But most developers use single capital letters or very short capital names to denote their template types, for example "T" or "TYPE".

Your function def_size has the wrong return type. If any it should have return type size_t (or int at best). As this function just sets the default size, it doesn't need any return type at all. I would declare it as
C++
void def_size ()


But there is yet another error in that function. It assigns a value to the size member variable, but it doesn't allocate the memory for array. Hence, you don't know whether size denotes the size of the array that you have already allocated or the size that should be allocated on the next occasion. There are two ways out of that dilemma:

(a) Always allocate first and then assign the size to your size variable. For exmaple:
C++
void set_default_size()
    {allocate (10);}
void allocate (size_t newSize)
{
    pData = new int[newSize];
    size = newSize;
    // there is still a BIG error in this function, which
    // we talk about later
}


(b) Use two variables, one for the currently allocated size and one for the default size:
C++
private:
    size_t m_size;       // size that we have currently allocated
    size_t m_defaultSize; // size to use as default value


As you see, I prefixed the size names with "m_". That is to help readers of your code to understand that these are member variables, which is not always obvious.

Finally, there is still a problem in the allocate function, above. I separated that code into a function, because it is a little more complex than just using the new keyword. What if the user calls this function several times? The old memory area is lost and with it the contents stored so far. This is called a memory leak. You will be exhausting the heap storage more and more. So you have to check if you have already allocated memory for your array. If yes, and the existing memory is smaller than the requested one, you will have to allocate a new and bigger storage area, then copy the old contents over to that new area and finally dispose of the old storage area by using delete[]. I don't give you the code for that, because this is your homework and a good exersize.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900