Best way to exchange array between derived class and base class

Base class has an array, whose size is controlled by the derived class.

I can't use the STL and use a 2003 compiler, so things like std::vector and std::array are out. I also can't use dynamic memory allocation.

So I thought of a few options:

1.
template <int N> class myBaseClass { ... int array[N]; ... }

then class MyClass: public myBaseClass<8> ... etc ...

2.
have a int **array in the base and assign in the derived class.

3.
give the base some virtual methods such as int *getArray or even int &getInt for more safety.

4.
Something else?

What does everyone think?
What do you mean by "exchange array"?


Your option 2 would assign something, yet dynamic allocation was not an option?
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.
There is also a question of when.

These are determined at compile time:
1
2
3
4
int myarray[4];

template <size_t N> class myBase { int array[N]; };
myBase<4> myarray;

This is "runtime":
1
2
3
4
5
class myBase {
  const size_t maxsize {99};
  int myarray[maxsize];
  size_t size;  // effective size
};

In the latter the size is where it belongs -- with the array -- and the derived classes are free to adjust it (via the interface of the base).
No STL and not dynamic allocation? Yikes.

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.
Last edited on
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?
Topic archived. No new replies allowed.