我是 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)
,m2
是Monkey
类型,名称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/