saving "this" in base class

M

Martin Drautzburg

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(this);

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?
 
V

Victor Bazarov

Martin said:
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(this);

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
 
M

Martin Drautzburg

Victor Bazarov wrote:

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.
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?
 
B

BobR

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<MyDerived*>( basepointer );
if( Md ){
Md->FuncOnlyInDerived();
}
else{
std::cerr<<"Not a derived object"<<std::endl;
}
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top