Smart pointer member variables

C

cleerline

Hi there,

I have the following code:

in .h file i have

class myclass
{
....
....
tile_list* my_tile_list;
....
....
}

in .cpp file I have

....
....
my_tile_list = new tile_list(a, b);
....
....

Now, i am trying to replace the pointers to tile_list with a smart
point (specificly the boost shared_ptr smart pointer)

Now, in the docs it says "replace all occurrances of new with
shared_ptr<T> p(new Y)

Now, I can do this when I have somthing like:

tile_list* x = new tile_list(a, b);

it becomes:

shared_ptr<tile_list> p(new tile_list(a,b));

it compiles and all is well.

HOWEVER, how do i do the same thing for the pointers which are member
variables?

They are declared and instantiated in different places.

I have tried the following:

class myclass
{
....
....
shared_ptr<tile_list> p;
....
....
}

in .cpp file I have

....
....
shared_ptr<tile_list> p(new tile_list(a,b));
....
....

but this does not work.

How do I do this??

thanks for any help.

Cleerline
 
L

Lance Diduck

Hi there,

I have the following code:

in .h file i have

class myclass
{
...
...
tile_list* my_tile_list;
...
...

}

in .cpp file I have

...
...
my_tile_list = new tile_list(a, b);
...
...

Now, i am trying to replace the pointers to tile_list with a smart
point (specificly the boost shared_ptr smart pointer)

Now, in the docs it says "replace all occurrances of new with
shared_ptr<T> p(new Y)

Now, I can do this when I have somthing like:

tile_list* x = new tile_list(a, b);

it becomes:

shared_ptr<tile_list> p(new tile_list(a,b));

it compiles and all is well.

HOWEVER, how do i do the same thing for the pointers which are member
variables?

They are declared and instantiated in different places.

I have tried the following:

class myclass
{
...
...
shared_ptr<tile_list> p;
...
...

}

in .cpp file I have

...
...
shared_ptr<tile_list> p(new tile_list(a,b));
...
...

but this does not work.

How do I do this??

thanks for any help.

Cleerline
Try p=shared_ptr<tile_list>(new tile_list(a,b));
or
p.reset(new tile_list(a,b) );
or
myclass::myclass():p(new tile_list(a,b)){}

Lance
 
C

cleerline

Try p=shared_ptr<tile_list>(new tile_list(a,b));
or
p.reset(new tile_list(a,b) );
or
myclass::myclass():p(new tile_list(a,b)){}

Lance

The first suggestions seems to have done the trick.

Many thanks Lance

Cleerline
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top