I used a third-part library in my company. In it, Some classes I need is defined like this:
class A
{};
class B : public A
{};
class C : public A
{};
class Foo
: public B
, public C
, public A
{};
And here, I need to gain offsets between Foo and all it's base classes. so I code like this:
int main()
{
Foo* f = new Foo();
int_ptr ptrb = ((B*)(Foo*)0x1) - 0x1;
int_ptr ptrc = ((C*)(Foo*)0x1) - 0x1;
int_ptr ptra = ((A*)(Foo*)0x1) - 0x1; // error
A *ptr = (A*)(Foo*)f; // error
cout << "Hello world!" << endl;
return 0;
}
in VC2010 and VC2012, it's all okay.
but in GCC 4.7.3, there will be a "Ambiguous base" compile-error.
I may not modify any code of the declaration. How could I gain the offset between Foo and the last "public A" ?
Your code can be simplified:
class A
{};
class B : public A
{};
class Foo
: public B
, public A
{};
Here the Foo
class has two A
base classes
A
named Foo::B::A
A
that you cannot name in C++You will be able get the B::A
base class subobject of a Foo
object:
Foo f;
B &b = f;
A &a = b;
but not the other A
impossible-to-name base class subobject.
You created an ambiguous inheritance situation.
Conclusion: do not do that.
Also: I really have no idea what you were really trying to do.