简体   繁体   中英

Extending inherited array in C++

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, ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM