【发布时间】:2016-07-12 10:11:45
【问题描述】:
#include <string>
using String = std::string;
class Base {
protected:
String value;
};
class Readonly : virtual Base {
public:
const String& method() const {
return value;
}
String& method() {
return value;
}
};
class Writeonly : virtual Base {
public:
Writeonly& method(const String& value) {
this->value = value;
return *this;
}
Writeonly& method(String&& value) {
this->value = std::move(value);
return *this;
}
};
class Unrestricted : public Readonly, public Writeonly {};
void usage() {
String string;
Unrestricted unrestricted;
unrestricted.method(string); // ambiguous
string = unrestricted.method(); // ambiguous
}
谁能向我解释一下为什么这些方法调用是模棱两可的?
将它们放在“Writeonly”或“Readonly”中时不会有歧义。
我想将此用于基于模板的访问器属性。因此,我希望能够使用“Writeonly”、“Readonly”和“Unrestricted”的实例。
【问题讨论】:
标签: c++ multiple-inheritance ambiguity member-functions name-lookup