0

I have pointer, assume it should be array of class A. this pointer is defined in Ancesor class, as [2]. it's set in constructor. the inheritor want to add 2 additional cells to this array, i.e the array shall be [4].

the constructor isn't offer the option to use overrided virtual methods. so, how can I extends the array in inheritor to be 4 instead of 2, without creating the 2 in the ancesor, delete it in inheritor, and create new 4?

BTW, it's not possible to use container such vector etc. it must be array.

4
  • Do any parent constructor allow you to provide the size? Commented Feb 25, 2015 at 8:57
  • Can you change the parent class (or does the parent class have any constructor which does not create the array)? If not, this isn't possible. Commented Feb 25, 2015 at 8:57
  • I'm not allowed to change the parent class. and the data is in the constructor, I can't modified it. Commented Feb 25, 2015 at 9:56
  • Mankarse, I guess you're right. in my current situation, I can't modify it. I thought maybe there's other way I havn't tought about, but it seems not. Thanks to all responders. Commented Feb 25, 2015 at 11:37

1 Answer 1

1

If you are setting the size of the array in the constructor, then the simplest solution is simply to overload the constructor.

If you create a new constructor with a parameter being the size of the array, then you can simply call that ancestor constructor from within the derived class' constructor.

In code:

Ancestor( unsigned int arraySize ){ array = new OBJECT[arraySize]; }

Then use that in the derived.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.