Intrusive pointer problem

Discussion in 'C++' started by Pradeep, Oct 5, 2005.

  1. Pradeep

    Pradeep Guest

    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
     
    Pradeep, Oct 5, 2005
    #1
    1. Advertising

  2. Pradeep wrote:
    > 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.
     
    Maxim Yegorushkin, Oct 5, 2005
    #2
    1. Advertising

  3. Pradeep

    Greg Guest

    Pradeep wrote:
    > 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
     
    Greg, Oct 5, 2005
    #3
  4. Pradeep

    Pradeep Guest

    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>
     
    Pradeep, Oct 5, 2005
    #4
  5. Pradeep

    Greg Guest

    Pradeep wrote:
    > 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
     
    Greg, Oct 5, 2005
    #5
  6. Pradeep

    Pradeep Guest

    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;
     
    Pradeep, Oct 5, 2005
    #6
  7. Pradeep wrote:
    > 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));
     
    Maxim Yegorushkin, Oct 5, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sabyasachi Basu

    Non-intrusive object serialization

    Sabyasachi Basu, Mar 7, 2004, in forum: C++
    Replies:
    1
    Views:
    630
    Claudio Puviani
    Mar 7, 2004
  2. Marcin Kalicinski

    Intrusive list

    Marcin Kalicinski, May 17, 2004, in forum: C++
    Replies:
    9
    Views:
    3,283
    Peter Koch Larsen
    May 18, 2004
  3. Charles Hartman

    intrusive posts

    Charles Hartman, Mar 24, 2005, in forum: Python
    Replies:
    0
    Views:
    323
    Charles Hartman
    Mar 24, 2005
  4. Pradeep
    Replies:
    1
    Views:
    300
    Jim Langston
    Oct 19, 2005
  5. Steven T. Hatton
    Replies:
    2
    Views:
    1,526
    Steven T. Hatton
    Nov 30, 2006
Loading...

Share This Page