0

For the following base, derive and list class, how to initialize each element using non-default constructor base(int newsize) instead of default constructor base() so that I immediately create a correct array size for each element in the list?

class base
{

    // default constructor
    base();

    // constructor to a newsize
    base(int newsize);


    int *array;
    int size;
};

class derive : public base
{

    int somethingelse;
};

class list
{

    // constructor to newlistsize element with newarraysize array
    list(int newlistsize, int newarraySize);

    derive *element;
    int listSize;
};


list::list(int newlistsize, int newarraySize)
{

    element = new derive [newlistsize];   
// how to initialize the array with newarraysize

}
billz
  • 44,644
  • 9
  • 83
  • 100

1 Answers1

0

You need to use initializer list.

class derive : public base
{
   public:
      derive(int size):base(size)
                   //  ^^^^^^^^^ invoking base class parameterized constructor
      {}
   .....
  1. Class members are private by default. You need to have public access specifier to the base and derive constructors for the above to work.
  2. Don't manage memory yourself. Use smart pointers instead. In which case, you don't have to worry about releasing the resource.
  3. What is rule of three ?
Community
  • 1
  • 1
Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • Note that the base constructor is private. Its possible that OP just provide a short code example, and forget it. But if not, warn him. – Manu343726 Jul 22 '13 at 23:50