C++ inheritance ambiguity

| 1 Comment | No TrackBacks
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.

No TrackBacks

TrackBack URL: http://www.robertwrose.com/mt/mt-tb.cgi/17

1 Comment

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 comment

Myself

Projects

Monthly Archives

Photos

About this Entry

This page contains a single entry by Robert W. Rose published on April 5, 2006 3:59 PM.

Where did my C/C++ settings go in Visual Studio? was the previous entry in this blog.

Hybrid Cars is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.