C ++虚拟覆盖函数具有相同的名称 [英] C++ virtual override functions with same name

查看:189
本文介绍了C ++虚拟覆盖函数具有相同的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的东西(简化)

I have something like that (simplified)

class A
{
  public:
    virtual void Function () = 0;
};

class B
{
  public:
    virtual void Function () = 0;
};

class Impl : public A , public B
{
  public:
        ????
};



如何实现A的Function()和B的Function
Visual C ++让你只能定义特定的函数inline(即不在cpp文件中),
,但我想它是一个扩展。 GCC抱怨这个。
是否有标准的C ++方法告诉编译器我要覆盖哪个函数?

How can I implement the Function () for A and the Function() for B ? Visual C++ lets you only define the specific function inline (i.e. not in the cpp file), but I suppose it's an extension. GCC complains about this. Is there a standard C++ way to tell the compiler which function I want to override?

(visual c ++ 2008)

(visual c++ 2008)

class Impl : public A , public B
{
  public:
     void A::Function () {  cout << "A::Function" << endl; }
     void B::Function () {  cout << "B::Function" << endl; }
};

谢谢!

推荐答案

您不能在那里使用限定名称。我写了 void Function(){...} 你正在覆盖两个函数。 Herb Sutter会显示如何解决

You cannot use qualified names there. I you write void Function() { ... } you are overriding both functions. Herb Sutter shows how it can be solved.

另一种选择是重命名这些函数,因为显然他们做了不同的事情(否则我看不到重写行为)。

Another option is to rename those functions, because apparently they do something different (otherwise i don't see the problem of overriding both with identical behavior).

这篇关于C ++虚拟覆盖函数具有相同的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆