这是有效的 C++ 吗? 代码?

发布于 2024-07-22 13:00:30 字数 539 浏览 2 评论 0原文

我只是想知道下面的代码块在 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;
  }
};

使用 VS2008 编译时没有任何错误,但是,在 GCC (MinGW) 3.4.5 上它给了我这样的错误:

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

在实现虚拟方法的行上。 我很好奇这是否通常被认为是无效的并且是 C++ 标准禁止的代码(在 VS 中,由于一些 MS 非标准化的魔法,它因此可以工作),或者只是 GCC 中的错误或不受支持的语言功能。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凑诗 2024-07-29 13:00:30

不,这是无效的。 您不能像这样单独覆盖它们,因为它们具有相同的签名。

关于这一点,有一位本周大师

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.

得不到的就毁灭 2024-07-29 13:00:30

不允许将限定名 A::b 作为 C 类成员的名称。

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

国粹 2024-07-29 13:00:30

由于父级具有相同的函数名称,它不应编译。 此外,您不应该多次重载函数 b 。

如果您引用为此创建的虚拟表:

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

您会看到,由于类 B 与类 A 具有相同的函数名称,因此它会覆盖它,因此您现在可以覆盖 B::b() (因为它仍然是纯的)。 这是由于多重继承造成的。 编译器如何区分两者(它们具有相同的签名)? 一般来说,这会失败,因为就像我刚才说的,编译器不应该做出决定,它应该告诉你存在问题。

它可以在 VS 上编译,但是您是否尝试过运行它(包含在实际创建它的文件中)? 有时,编译器很懒,不会在未使用的类上弹出错误。

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.

佞臣 2024-07-29 13:00:30

仅供参考,仅当您尝试使用 b 方法时,VC 才会给出错误:

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)'

并且值得一提的是,Comeau 的编译器的行为类似,但有一个更令人困惑的错误消息:

"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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文