-2

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.

    template<typename stuff> //my template
    class safearray
    {
    public:
    int size;
    stuff*data[size];
    
    safearray def_size() // default size constructor
        {int size = 0; return size;}
    
    
    safearray new_size(new_int) 
        {delete [] data;
         data = new int [newsize]; 
         size = newsize; }
    

Mostly I would like to know if I am on the right track here, I'm new to programming so I'm not sure if all the syntax is right. Any help would be apreciated

jack
  • 1
  • 2
  • 1
    `stuff*data[size];` <- no, you can't do this. `stuff *data;` with `new` in constructor. – crashmstr Jan 30 '15 at 16:52
  • 3
    I recommend you check [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), because you apparently need to learn some basic C++ in regards to constructors first. – Some programmer dude Jan 30 '15 at 16:53
  • Another teacher believing that `int` is a proper type for sizes... *sigh*... – danielschemmel Jan 30 '15 at 16:59
  • Neither of these method definitions is a constructor. Start from basic C++. – zoska Jan 30 '15 at 17:01
  • `My assignment is to create a class that implements a safe resizable array,` I'm going to bet that no one in your class completes this assignment. Sorry to be a downer, but I have yet to see a student code anything like this correctly (correctly meaning copyable, assignable, exception safe, etc.) without the help of experienced coders or experts. – PaulMcKenzie Jan 30 '15 at 17:06
  • 1
    implement it using std::vector – pm100 Jan 30 '15 at 17:14
  • def_size is not a constructor. a constructor is a method with the same name as the class – pm100 Jan 30 '15 at 17:17

1 Answers1

3

Well, first off, it might be a good idea to use proper naming and some conventions:

  • stuff template parameter doesn't really tell the user what is it doing. Consider using something like Element or TElement. Or T if you're not in a creative mood.
  • safearray is not really legible, consider using something that can help reader distinguish the words, e.g. safe_array, SafeArray

Then, it would be good to work on some indentation. You can either do

void SomeMethod() {
    // code here
}

or

void SomeMethod()
{
    // code here
}

Next, your idea of what is constructor is wrong. The basic idea is that constructor is a method that has no return type and has the same name as containing class, e.g.

class SomeClass
{
private:  // private marks the internal state of the class
          // it is inaccessible from the outside of the class
    int someValue;
    // it might be a good idea to store the size of the array here
    // so the user cannot modify the size

public:
    SomeClass()
    {
        someValue = 0;
    }

    SomeClass(int value)
    {
        someValue = value;
    }
};

And then you obviously call constructor by using the type:

void SomeMethod()
{
    SomeClass cl1; // creates an instance of SomeClass on stack calling the
                   // parameter-less constructor

    SomeClass cl2(7); // creates an instance of SomeClass on stack with
                      // the parametrized constructor

    SomeClass* cl3 = new SomeClass(12); // creates an instance of SomeClass on heap 
                                        // with the parametrized constructor

    delete cl3; // remember to delete everything that has been created with new
}

If you need to create an array of something, you would do

size_t size = 6; // your required size comes here, e.g. constructor parameter
int* array = new int[size];

Having template parameter T, you would obviously need to do

T* array = new T[size];

So this was really a fast and not very precise walkthrough on this stuff. If I were you, I'd take the recommended books in comments and start learning from scratch. This level of assignment is crazy for your level of skill and won't give you any deeper understanding of programming or C++ at all.

PS. I'm a student :)

Zdeněk Jelínek
  • 2,611
  • 1
  • 17
  • 23
  • 1
    Yeah, but your solution requires default constructor for type T and this is not quite good. It would be easier to implement it using `std::vector` and use `std::vector::reserve()`, when just reserving space and not putting elements. – zoska Jan 30 '15 at 18:19
  • Thanks for the good point! – Zdeněk Jelínek Jan 30 '15 at 21:46