Help with intrusive_ptr

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with intrusive_ptr

QbProg
Hello,
I use intrusive_ptr with a class that holds the refcount and I derive
from that class all my other classes.

class TObject {
 public :
int refcount ;

TObject () { refcount = 0;}


}
I have defined the intrusive_..._addref and _release  to increase and
decrease/delete that refcount and everything works good!

Anywhay sometimes I need to assign a derived object to another derived
object of the same type

instrusive_ptr <Derived> A = new Derived;
.
.
.
the problem:
*A = *Derived(constr parameters).
The point is that the refcount is assigned too (and becames 0)

How are instrusive_addref and _release supposed to be implemented ?

Any quick and clean solution for that ? I cannot write assignment
operator for every derived class because... there are a lot of members
:)

Really thanks!
Thomas
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Help with intrusive_ptr

Alan M. Carroll
Define a copy contructor and assignment operator for the base class that does not copy/assign the reference count. Subclasses will use that for the members defined in the base class, i.e. it will solve the problem for all derived classes.

If you want it very clean, write a class that contains only the reference count with do-nothing copy/assignment and then use that as a base class for your current base class.

At 12:22 PM 3/17/2006, Thomas Laguzzi wrote:

>Hello,
>I use intrusive_ptr with a class that holds the refcount and I derive
>from that class all my other classes.
>
>class TObject {
> public :
>int refcount ;
>
>TObject () { refcount = 0;}
>
>
>}
>I have defined the intrusive_..._addref and _release  to increase and
>decrease/delete that refcount and everything works good!
>
>Anywhay sometimes I need to assign a derived object to another derived
>object of the same type
>
>instrusive_ptr <Derived> A = new Derived;
>.
>.
>.
>the problem:
>*A = *Derived(constr parameters).
>The point is that the refcount is assigned too (and becames 0)
>
>How are instrusive_addref and _release supposed to be implemented ?
>
>Any quick and clean solution for that ? I cannot write assignment
>operator for every derived class because... there are a lot of members
>:)
>
>Really thanks!
>Thomas
>_______________________________________________
>Boost-users mailing list
>[hidden email]
>http://lists.boost.org/mailman/listinfo.cgi/boost-users

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users