This is a really bad design. Why do you need a constructor that accepts int* in your Base class when there is no element to initialize?
Taken from your comment on Pontus' answer, it seems you are aware of this flaw.
class Base { private: int array[3]; public: Base(int* arr); virtual ~Base(); }; class Derived : Base { public: Derived(); };
Then you pass the array back to the base class using initialization lists:
Derived() : Base(new int[3]) { array[0] = array[1] = array[2] = 1; }
Basically, you call the class Base constructor and pass a parameter. And the Base constructor will also use the initialization list:
Base(int* arr) : array(arr) { }
In addition, when the Derived constructor starts, the Base object is already fully initialized, as promised by the standard.
Of course, you have to handle the destruction of your dynamically allocated array in Base :
virtual ~Base(){ delete [] array; }
Greetings.
source share