Get address of whole instance with multiple inheritance
Hi,
is there any way to get a pointer to the "whole" instance after
casting to a base class.
i.E.:
class Interface1 {
public:
virtual void foo() = 0;
};
class Interface2 {
public:
virtual void bar() = 0;
};
class Interface3 {
public:
virtual void baz() = 0;
};
class Impl : public Interface1, public Interface2, public Interface3 {
public:
virtual void foo() {
printf("Impl::foo() : this = %p\n", this);
}
virtual void bar() {
printf("Impl::bar() : this = %p\n", this);
}
virtual void baz() {
printf("Impl::baz() : this = %p\n", this);
}
};
int main(int argc, char** argv) {
Impl* impl = new Impl();
printf("impl = %p\n", impl); // impl = 0x603010
Interface1* if1 = impl;
printf("if1 = %p\n", if1); // if1 = 0x603010
Interface2* if2 = impl;
printf("if2 = %p\n", if2); // if2 = 0x603018
Interface3* if3 = impl;
printf("if3 = %p\n", if3); // if3 = 0x603020
if1->foo(); // Impl::foo() : this = 0x603010
if2->bar(); // Impl::bar() : this = 0x603010
if3->baz(); // Impl::baz() : this = 0x603010
return 0;
}
as you can see the cast from Impl to any Interface changes the
address, but if a virtual method is called
the this pointer points to the instance in which the method is
implemented.
During a method call C++ will use the vtable (I think) but is there
any way to get the address of the whole instance (the address returned
by new) without knowing the concrete class.
What I need is:
void* ptr = some_magic(if2); // Or any other interface
and ptr should now point to impl(0x603010), not to if2(0x603018).
Is there a common name for that pointer (something I can use to search
for) or do you have idea how I can do that?
greetings
Dustin
--
$ g++ -v
Target: x86_64-pc-linux-gnu
gcc version 4.3.2 (Gentoo 4.3.2 p1.0)
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]