Intrusive pointer problem

P

Pradeep

Hi,

I am using intrusive pointers for my code but i am not much aquianted
with them. I am using an example to illustrate my problem as i can't
share the code.

I have two clases ( say A and B)
B is derived from A( B: public A)

now i want to pass the this* pointer of the derived class(B) to a
method of an object that takes intrusive pointer of the base class(A)
as argument.

method definition
pass(intrusive_ptr<A>)
and i am passingt the this pointer of class B.

I have tried using dynamic_cast as well as dynamic_pointer_cast but i
still get the error.

Thanks in Advance.
Pradeep
 
M

Maxim Yegorushkin

Pradeep said:
Hi,

I am using intrusive pointers for my code but i am not much aquianted
with them. I am using an example to illustrate my problem as i can't
share the code.

I have two clases ( say A and B)
B is derived from A( B: public A)

now i want to pass the this* pointer of the derived class(B) to a
method of an object that takes intrusive pointer of the base class(A)
as argument.

method definition
pass(intrusive_ptr<A>)
and i am passingt the this pointer of class B.

I have tried using dynamic_cast as well as dynamic_pointer_cast but i
still get the error.

With boost::intrusive_ptr<> it should work.

You might like posting a complete example, so that people here can see
the behaviour you observe.
 
G

Greg

Pradeep said:
Hi,

I am using intrusive pointers for my code but i am not much aquianted
with them. I am using an example to illustrate my problem as i can't
share the code.

I have two clases ( say A and B)
B is derived from A( B: public A)

now i want to pass the this* pointer of the derived class(B) to a
method of an object that takes intrusive pointer of the base class(A)
as argument.

method definition
pass(intrusive_ptr<A>)
and i am passingt the this pointer of class B.

I have tried using dynamic_cast as well as dynamic_pointer_cast but i
still get the error.

What error?

Greg
 
P

Pradeep

When i do

pass( boost::intrusive_ptr<A> (*this));

where A is the derived class while the argument for the method is
intusive pointer to the base class

I get the error C2440: Cannot convert from const A to
boost::intrusive_ptr<T>
 
G

Greg

Pradeep said:
When i do

pass( boost::intrusive_ptr<A> (*this));

where A is the derived class while the argument for the method is
intusive pointer to the base class

I get the error C2440: Cannot convert from const A to
boost::intrusive_ptr<T>

I believe you want to write:

pass( boost::intrusive_ptr<A>(this));

or even

pass(this);

Smart pointers are constructed from pointers - "this" is a pointer,
"*this" is a value.

Greg
 
P

Pradeep

Thanks Greg.

I change the this* to this. However i still get the error. 2440 -
cannot convert frm A *const to boost::intrusive_ptr<T>

the definition of the method is

void pass(const BPtr& bptr)

where BPtr is typedef boost::intrusive_ptr<B> BPtr;
 
M

Maxim Yegorushkin

Pradeep said:
When i do

pass( boost::intrusive_ptr<A> (*this));

where A is the derived class while the argument for the method is
intusive pointer to the base class

There is no implicit conversion from a base class pointer to a derived
one.
You have to cast it explicitly:

struct A {}; struct B : A {};
void foo(B*);

A* a;
foo(static_cast<B*>(a));
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top