Tutorial :Is this valid C++ code?


I just wanted to know whether is this following block of code fully valid in C++:

class A  {  public:     virtual bool b() = 0;  };    class B  {  public:     virtual bool b() = 0;  };    class C: public A, public B  {  public:    virtual bool A::b()    {      return true;    }      virtual bool B::b()    {      return false;    }  };  

Using VS2008 it compiles without any errors, however, on GCC (MinGW) 3.4.5 it gives me errors like:

cannot declare member function `A::b' within `C'  

On the lines where the virtual methods are implemented. I was curious if this is just generally considered invalid and by C++ standards forbidden code (and in VS it thus works thanks to some MS non-standardized magic), or only a bug or unsupported language feature in GCC.


No it's not valid. You can't override them separately like that, because they would have the same signature.

There is a guru of the week about that.


The qualified name A::b is not allowed as the name of a member of class C.


It should not compile due to parents having the same function name. Furthermore, you aren't suppose to overload the function b more than once.

If you refer to the virtual table created for this:

0(beginning of vtable) - A::b() -> B::b()  

You see, since class B has the same function name as class A, it overrides it, thus you have now B::b() to override (since it's still pure). This is due to multiple inheritancy. How would the compiler be able to differenciate between the two (they have the same signature)? Generally, this will fail, because like I just said, the compiler isn't suppose to make decisions, it's supposed to tell you that there is a problem.

It compiles on VS, but have you tried running it (included in a file where it is actually created)? Sometimes, compiler are lazy and don't pop errors on classes that aren't used.


Just as an FYI, VC gives the error only when you try to use the b method:

C:\temp\test.cpp(33) : error C2668: 'C::b' : ambiguous call to overloaded function        C:\temp\test.cpp(23): could be 'bool C::b(void)'        C:\temp\test.cpp(18): or       'bool C::b(void)'        while trying to match the argument list '(void)'  

And for what it's worth, Comeau's compiler behaves similarly, but with an even more confusing error message:

"C:\temp\test.cpp", line 33: error: no instance of overloaded function "C::b"            matches the argument list              object type is: C        bool z = c.b();  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »