C++ inheritance ambiguity

Here’s something fun I discovered today:


class a {
public:

class b {
public:
virtual void dosomething() = 0;
};
};

class c : public a::b {
public:

class b {
public:
virtual void dosomethingelse() = 0;
};

void dosomething() {}
};

class d : public c::b {
public:

void dosomethingelse() { printf ("do something else\n"); }
void dosomething() { printf ("do something\n"); }
};

void main()
{
d *foo = new d();
c::b *myfoo = reinterpret_cast(foo);
myfoo->dosomethingelse();
}

With Visual Studio .NET 2003 you’ll see “do something” on the console. With GCC you’ll see “do something else”. In fact, with GCC you don’t even need the reinterpret_cast.

One Reply to “C++ inheritance ambiguity”

  1. Hi Robert,
    Funny story, we were searching for a solution to a problem (trying to implement an interface that was defined as virtual someobject* getSomething() const;) and google pulled up this post as one of the top 10 results. Sally clicked on it, not even knowing who you are. 🙂
    Still helping HP even though you don’t work here anymore.

Leave a Reply to joseph.a.davis Cancel reply