c++方法调用(左值)绑定(bind)到派生类中的函数(右值)而不是基类中的函数(左值)

标签 c++ binding virtual lvalue rvalue

我是 C++ 的新手,正在尝试编写如下接口(interface):

template <class T>
class Comparable
{
   protected:
      Comparable(){};
   public:
      virtual int compare(const T&&)=0;
      int compare(const T& o)
      {
         return this->compare(std::move(o));
      }
};

我这样做是为了尝试让比较方法同时处理两个 l/r 值。 我从 Comparable 派生了以下类:

class Monkey : Comparable<Monkey>
{
   private:
      char name[512];
   public:
      Monkey(const char*&& name)
      {
         strcpy(this->name, name);
      }
      const char *getName() { return name; }
      int compare(const Monkey&& m)
      {
         return strcmp(name, m.name);
      }
};

使用 main() 方法如下:

Monkey m1(argv[1]), m2(argv[2]);
printf("\"%s\" \"%s\" %d\n", m1.getName(), m2.getName(), m2.compare(m1));

但是我得到一个编译错误:

无法将“Tests::Monkey”左值绑定(bind)到“const Tests::Monkey&&” 初始化 'virtual int Tests::Monkey::compare(const Tests::Monkey&&)' 的参数 1 构建失败

为什么方法调用没有绑定(bind)到基类中的compare方法?

当我在基类中将它们都创建为虚拟并将它们都写入派生类时,绑定(bind)工作正常,但它不起作用,如果我尝试按照此处编写的方式编译它,我会收到编译错误.

最佳答案

这是名字隐藏;派生类中的名称隐藏了基类中的名称。简而言之,您不能通过不同的作用域重载函数。

根据unqualified name lookup的规则,对于m2.compare(m1)m2Monkey类型,名称compare会在首先是派生类 Monkey 的范围,然后停止名称查找。基类中的 compare 根本不会被考虑用于以下重载决议。

(强调我的)

For an unqualified name, that is a name that does not appear to the right of a scope resolution operator ::, name lookup examines the scopes as described below, until it finds at least one declaration of any kind, at which time the lookup stops and no further scopes are examined.

您可以通过使用将基类的名称引入派生类的范围:

class Monkey : Comparable<Monkey>
{
      ...

      using Comparable<Monkey>::compare;
      int compare(const Monkey&& m)
      {
         return strcmp(name, m.name);
      }
};

关于c++方法调用(左值)绑定(bind)到派生类中的函数(右值)而不是基类中的函数(左值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42780911/

相关文章:

c++ - 链表覆盖之前的值

wpf - 如何绑定(bind)到渐变停止 WPF

javascript - AngularJS:将文本区域绑定(bind)到对象属性 - infdig 错误

c++ - 虚函数题

c++ - 访问多维数组元素c++

c++ - WinAPI WC_LISTVIEW 绘制问题

c++ - 使用 std::function 的重载解析

Java动态绑定(bind)

c++ - 我可以在这里使用 Curiously Recurring Template Pattern (C++) 吗?

c# - 虚拟方法逻辑不工作 C# .NET 4.0