[corrected]How to delay the initialisation of a member in a C++ base class until the ctor of derived class is executed?

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 { public: base():size(), p_array(0){} private: size_t size; boost::shared_array<unsigned char> p_array; }; in the derived class: class derived { public: 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

2009/3/25 tom tan
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 { public: base():size(), p_array(0){} private: size_t size; boost::shared_array<unsigned char> p_array;
};
in the derived class:
class derived { public: derived(size_t array_size):size(array_size) { p_array.reset(new unsigned char[size]); } };
There are still many mistakes: - derived does not inherit from base - size is not a member of derived and thus cannot be initialized in its constructor Matthieu -- Information System Engineer, Ph.D. Website: http://matthieu-brucher.developpez.com/ Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn: http://www.linkedin.com/in/matthieubrucher

Is there any C++ design pattern could achieve it? Thanks
Since you're passing the size into the derived constructor, why don't you just pass it through to the base constructor? class base { public: base(size_t array_size):size(array_size) { p_array.reset(new unsigned char[size]); } private: size_t size; boost::shared_array<unsigned char> p_array; }; class derived { public: derived(size_t array_size):base(array_size) { } }; Will that work for you?

should work, but don't forget to derive 'derived' from base:
class derived : base
{...}
On Wed, Mar 25, 2009 at 10:13 AM, Alex MDC
Is there any C++ design pattern could achieve it? Thanks
Since you're passing the size into the derived constructor, why don't you just pass it through to the base constructor?
class base { public: base(size_t array_size):size(array_size) { p_array.reset(new unsigned char[size]); } private: size_t size; boost::shared_array<unsigned char> p_array; };
class derived { public: derived(size_t array_size):base(array_size) { } };
Will that work for you?
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Alex MDC
-
Matthieu Brucher
-
Ovanes Markarian
-
tom tan