
Also, shared_ptr won't work for arrays, you'll want to used
"shared_array" instead.
You might also think twice about this, and decide if having a shared
copy of the buffer is really what you want, or if you're just using
the smart pointer for memory management. If so, consider using
"scoped_array" and defining a copy constructor with the appropriate
semantics.
If you go this route, you may just as well stick with 'vector'. It's
less work, and probably far less overhead than you think.
On Wed, Mar 25, 2009 at 1:38 AM, Matthieu Brucher
Perhaps simply:
class base { public: base(size_t size):size(size), p_array(new unsigned char[size]){} private: size_t size; boost::shared_ptr<unsigned char> p_array; };
And then:
class derived:public base { public: derived(size_t array_size):base(array_size) { } };
Do not declare size and p_array again, they will be different than the ones in the base class...
Matthieu
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_ptr<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]); } private: size_t size; boost::shared_ptr<unsigned char> p_array;
};
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. Is there any C++ design pattern could achieve it? Thanks
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- 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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users