[C++] 为什么operator = 不能被继承

class A{
public:
	A& operator += (const A&) {return *this;};
	A& operator = (const A&) {return *this;};
};

class B:public A{
public:
	B(){};
	virtual ~B(){};
};

int main(){
	A a;
	B b;
	b +=a;
	b = a; //invalid
	return 0;
}

 

为什么 b += a;可以编译通过,但是b = a不行呢 ??

因为operator =很特殊,即使你没有写,compiler也要生产一个,所以已经有一个类型为

B& operator B(const B&)的函数在哪里了。

posted @ 2012-06-21 15:21  嗷嗷  阅读(795)  评论(0编辑  收藏  举报