?
=?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=
Hello!
Please consider the code below:
class A {
public:
virtual void foo(int i) = 0;
};
class B {
public:
virtual void bar(int i) = 0;
};
class AB : public A, public B {
public:
void foo(int i) { }
void bar(int i) { }
};
int main() {
AB* ab = new AB();
cout << ab << endl;
cout << dynamic_cast<A*>(ab) << endl;
cout << dynamic_cast<B*>(ab) << endl;
return 0;
}
This gives me the output:
0x8049d90
0x8049d90
0x8049d94
Even though I point to the same object I get different values for my
pointer. I can understand that this might happen when I use multiple
inheritance and have more than 1 v-table (vtabl?).
The question is this: can I rely on this behaivour and expect to get
differnt pointers on all platforms or are there some platforms that
might give me the same pointer no matter on how I look at an object?
I suspect that this is not portable behaivour and that it's completely
undefined and compiler depandant. It would be nice to hear your comments.
Regards,
Mattias
Please consider the code below:
class A {
public:
virtual void foo(int i) = 0;
};
class B {
public:
virtual void bar(int i) = 0;
};
class AB : public A, public B {
public:
void foo(int i) { }
void bar(int i) { }
};
int main() {
AB* ab = new AB();
cout << ab << endl;
cout << dynamic_cast<A*>(ab) << endl;
cout << dynamic_cast<B*>(ab) << endl;
return 0;
}
This gives me the output:
0x8049d90
0x8049d90
0x8049d94
Even though I point to the same object I get different values for my
pointer. I can understand that this might happen when I use multiple
inheritance and have more than 1 v-table (vtabl?).
The question is this: can I rely on this behaivour and expect to get
differnt pointers on all platforms or are there some platforms that
might give me the same pointer no matter on how I look at an object?
I suspect that this is not portable behaivour and that it's completely
undefined and compiler depandant. It would be nice to hear your comments.
Regards,
Mattias