| re: "Static" destructor
Jahn Otto Næsgaard Andersen wrote:
[color=blue]
> Hi,
>
> I have a class A with a static pointer to some data. The pointer is static
> because I want to access the data from all instances of the class. The data
> itself is allocated when the first instance of A is constructed:
>
> # a.h
>
> class A
> {
> private:
> static char* sm_data;[/color]
If you can replace this with
static std::string sm_data;
then do so. It will save you headaches, and solve your immediate problem.
Another alternative is to use a smart pointer here. Something similar to
std::auto_ptr should work, but auto_ptr itself is not appropriate,
because it will use 'delete' rather than 'delete[]'. You'd have to
define your own 'auto_array' or use one from an existing library.
Using a smart pointer would let the rest of the code remain basically
untouched, so it's a good "quick fix". However, quick isn't always the
best way. Your overall design would probably benefit from switching to
something that allocates and manages memory for you, such as the
std::string suggested above.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. |