【发布时间】:2011-11-13 13:28:11
【问题描述】:
class A {
private:
A& operator=(const A&);
};
class B : public A {
public:
B& operator=(const A&) {
return *this;
}
};
int main() {
B b1;
B b2;
b1 = b2;
return 0;
}
这会在编译时出错:
test.cpp: In member function 'B& B::operator=(const B&)':
test.cpp:16:5: error: 'A& A::operator=(const A&)' is private
test.cpp:19:20: error: within this context
test.cpp: In function 'int main()':
test.cpp:31:7: note: synthesized method 'B& B::operator=(const B&)'
first required here
Build error occurred, build is stopped
由于 B::operator=(A&) 具有非标准签名,编译器生成它自己的 B::operator=(B&),它(尝试)调用 A::operator(A&),它是私有的。
有什么方法可以让编译器也将 B::operator=(A&) 用于 B 参数?
【问题讨论】:
标签: c++ inheritance operator-overloading