
25 Mar
2009
25 Mar
'09
10:04 a.m.
From: 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_array 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... but in this case array_size is re-assigned after p_array was allocated, thus not reflecting the size of the allocated buffer; What I wished to achieve is: 1) the buffer is allocated inside the base class while 2) the size is designated inside the derived class. >From: Christopher Currie >Also, shared_ptr won't work for arrays, you'll want to used >"shared_array" instead. That's right, I was thinking of shared_array while I was typing, but ... >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. I need some dynamically allocated space that can later be casted to C pointers, so that it can be passed to a Win32 API function that accepts C pointers only. vectors are dynamic, but they don't cast to C arrays naturally. In fact, I am wrapping that API in C++. is there a better way?