Basically I want the array in the base class, but the size of the array defined by the derived class.
Correct, dynamic allocation is not an option.
Option 2 would have the derived class do something like:
int myarray[4]={1,2,3,4};
base::array = &myarray;
But I think this is unclean and messy coding. I think the best way would be for the base class to call virtual methods that the derived class implements. Wondering if anyone else agrees.
Without dynamic allocation, the only way to have the parent class own the array is to template it. But then that destroys the possibility of polymorphism (since BaseClass<5> and BaseClass<10> are different classes.... you can't have a general 'BaseClass' pointer).
Which means you'd probably need to have the child class own the array.
Rather than giving a pointer to the parent, maybe have the parent access the array through get/set functions that the child class implements.
I dunno... this kind of reeks. I'm not sure I like any of these solutions.
keskiverto: I'm not sure what you mean by your second example. I don't see how the derived class is free to adjust the size of the array in the base class. Or do you mean just allocating 99 and setting the effective size in the derived class? That seems kinda wasteful.
With the template solution, the problem is that the entire code of the class that is templated must be present in the header file for the compiler to instantiate the various versions of the class.
The entire code has to be present, but only the necessary bits are actually used.
Yes, it is "wasteful" to allocate more than is actually needed. That is the old static C approach; you have a program that is hardcoded to handle data up to certain size. std::vector does that too; (dynamically) allocates a block for data and separately keeps track of how much is actually used.
What is the purpose of the base class? Common interface or common implementation?