Boost logo

Boost Users :

Subject: [Boost-users] [corrected]How to delay the initialisation of a member in a C++ base class until the ctor of derived class is executed?
From: tom tan (ttan_at_[hidden])
Date: 2009-03-25 05:02:08

Here's the scenario: I want define a base class that allocate some
buffer once for all derived classes, while the size of the buffer varies
among different derived classes. I could achieve it in this way:

class base
     base():size(), p_array(0){}
     size_t size;
     boost::shared_array<unsigned char> p_array;


in the derived class:

class derived
     derived(size_t array_size):size(array_size)
          p_array.reset(new unsigned char[size]);

However, in order to simplify the design of the derived class, I really
want to put this line:

p_array.reset(new unsigned char[size]);

to somewhere in the base class, thus writing it only once.

In other words,I wished to achieve this:
1) the buffer is allocated inside the base class while
2) the size is designated inside the derived class.

Is there any C++ design pattern could achieve it? Thanks

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at