Is this really C ++ code?

I just wanted to find out if this following code in C ++ is completely:

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'

In lines where virtual methods are implemented. I was curious if this is usually considered invalid and forbidden by C ++ standards code (and in VS it works thanks to some non-standard MS magic) or just an error or unsupported language function in GCC.

+3
source share
4 answers

No, this is not valid. You cannot redefine them separately because they will have the same signature.

.

+17

A:: b C.

+1

- , . , b .

:

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

, B , A, , B:: b() ( ). . ( )? , , , , , , .

It compiles on VS, but you tried to run it (included in the file where it was actually created)? Sometimes the compiler is lazy and does not throw errors in classes that are not used.

+1
source

Like FYI, VC only gives an error when trying to use a method b:

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, the Comeau 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();
0
source

Source: https://habr.com/ru/post/1709915/


All Articles