Click to See Complete Forum and Search --> : Assembly : get "this" pointer of the parent object


October 20th, 1999, 08:03 AM
Hi, all,

I'd like to create a base class "CBase" that will be a base for all my
classes.
CBase would have to obtain the "This" pointer of the parent object.

Suppose we have a class A containing an object (not a pointer to) "Dummy" of
class B.
Class B itself is derived from CBase.

I 'd like the "Dummy" object to automatically get a pointer to the class A
object, at the contruction time.

Viewing the assembly (Visual C++) of a constructor, I could see that ESP is
saved to FS:0 before the constructor call the constructor of any class
object or the constructor of any base class.
But, the problem is that I can't know the position of "This" from the ESP
register.

Anyone have an idea ?

Thanks a lot.

October 20th, 1999, 12:05 PM
The FS segment register points to a thread information block. So the FS:[0] stuff is for exception handling, not related to class information.

Visual C++ is known to pass this pointer in the ECX register. You can find external calls set ECX register before calling. I'm not so sure in the constructor, whether the object is properly initialized. For example, the vtable pointer may not be the final one.

Paul McKenzie
October 20th, 1999, 12:24 PM
I hope that your question is concerning class design and not manipulating assembly language. Unless you are writing an assembly version of the semantics of object construction, I don't know why you need to look at assembly code (one good reason to not look at the generated code is that the code generation may change with the next major or minor release of the compiler).

You also have a confusing question concerning class A. You stated that all of your classes are derived from CBase, but imply that A isn't derived from CBase. Also, the obvious answer is this: why not derive all your classes from CBase anyway?

Anyway, here is my quick analysis, but you will have to provide more info on what you are trying to do:

By giving "B" a pointer to A, you are implying that either "B" will know about the semantics of the A class (why would you be passing a pointer "A" to "B" if you didn't want to use "A" within B in some specific way)

OR

A is a pointer to a base class that B knows about, and B will possibly call virtual functions (in this case B doesn't really know it is dealing with an A object, but knows that the pointer passed to B is a base class pointer of a known object).

Basically, the intent that you are trying to achieve can be done using good class design, and I believe you really don't have any need to get into assembly language.

Regards,

Paul McKenzie