Boost logo

Boost Users :

Subject: Re: [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: Matthieu Brucher (matthieu.brucher_at_[hidden])
Date: 2009-03-25 05:10:16


2009/3/25 tom tan <ttan_at_[hidden]>:
> 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

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net