saving "this" in base class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Drautzburg

    #1

    saving "this" in base class

    For reasons related to python/swig and garbage collecting I want to
    remember every object in a linked list. I have a common base class
    RCObj which in turn has a static std::list<RCObj *memory.

    My problem is that I cannot get RCObj store the true this pointer. Even
    if I pass it explicitly from a derived class (called "Part") as in:

    Part::Part() {
    this->RCObj::init(th is);

    the pointer that arrives at RCObj::init() is not the same as "this"
    inside Part::Part()

    (gdb) p this
    $4 = (pyramid::Part * const) 0x81b62e0
    (gdb) s
    RCObj::init (this=0x81b6378 , that=0x81b6378) at common.cc:65

    I assume C++ treats an object as composed of several sections one of
    which is the RCObj section and is silently add an offset (0x81b6378 -
    0x81b62e0 = 152) to "this" if this shall be treated as an RCObj.

    (1) what happens when I "delete this" from within the RCObj scope? Will
    it only delete part of the object?
    (2) how can I possibly keep track of all my created objects?
  • Victor Bazarov

    #2
    Re: saving &quot;this&quot ; in base class

    Martin Drautzburg wrote:
    For reasons related to python/swig and garbage collecting I want to
    remember every object in a linked list. I have a common base class
    RCObj which in turn has a static std::list<RCObj *memory.
    >
    My problem is that I cannot get RCObj store the true this pointer.
    Why would you need to?
    Even if I pass it explicitly from a derived class (called "Part") as
    in:
    >
    Part::Part() {
    this->RCObj::init(th is);
    >
    the pointer that arrives at RCObj::init() is not the same as "this"
    inside Part::Part()
    Of course it isn't. So?
    (gdb) p this
    $4 = (pyramid::Part * const) 0x81b62e0
    (gdb) s
    RCObj::init (this=0x81b6378 , that=0x81b6378) at common.cc:65
    >
    I assume C++ treats an object as composed of several sections one of
    which is the RCObj section and is silently add an offset (0x81b6378 -
    0x81b62e0 = 152) to "this" if this shall be treated as an RCObj.
    >
    (1) what happens when I "delete this" from within the RCObj scope?
    If your destructor is virtual (as it should be), the object is
    destroyed correctly and the memory is freed. You shouldn't attempt
    to access any non-static members of that object after that point.
    Will it only delete part of the object?
    Not if the destructor is virtual.
    (2) how can I possibly keep track of all my created objects?
    There are different schemes for that, you should look on the Web
    for the available solutions.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Martin Drautzburg

      #3
      Re: saving &quot;this&quot ; in base class

      Victor Bazarov wrote:

      >the pointer that arrives at RCObj::init() is not the same as "this"
      >inside Part::Part()
      >
      Of course it isn't. So?
      I guess you are trying to tell me that the "this" pointer that arrives
      at the base class is good enough for all practical issues as virtual
      methods will always be dispatched correctly. So the actual value of
      this is of no concern.
      >(2) how can I possibly keep track of all my created objects?
      >
      There are different schemes for that, you should look on the Web
      for the available solutions.
      Haven't found anything yet. What exactly should I look for.

      Currently I am having problems calling a method that was not declared in
      the base class. So whenever I fetch an object from the
      static std::list<RCObj *theObjects;
      object pool I will only get an RCObj which knows nothing about methods
      implemented in its derived classes, even though the object *is* really
      of a derived class.

      I am thinking about providing virtual conversion functions in the RCObj
      base class like asMidiByte(), asNoteOn() and whatever derived classes I
      have. So the base class would only have to know about derived classes
      and not about each and every method in them. But I haven't tried this
      yet. What do you think?



      Comment

      • BobR

        #4
        Re: saving &quot;this&quot ; in base class


        Martin Drautzburg wrote in message...
        >
        Currently I am having problems calling a method that was not declared in
        the base class. So whenever I fetch an object from the
        static std::list<RCObj *theObjects;
        object pool I will only get an RCObj which knows nothing about methods
        implemented in its derived classes, even though the object *is* really
        of a derived class.
        The object is 'sliced'.
        Do you know about 'downcasting'?

        RCObj *basepointer( /* init it */ );

        MyDerived *Md = dynamic_cast<My Derived*>( basepointer );
        if( Md ){
        Md->FuncOnlyInDeri ved();
        }
        else{
        std::cerr<<"Not a derived object"<<std::e ndl;
        }

        --
        Bob R
        POVrookie


        Comment

        Working...