派生类是否间接继承基类的赋值运算符?
我试图理解这种行为,但似乎我不明白。请看这段代码:
#include <iostream>
using namespace std;
class Base
{
public:
void operator=(const Base& rf)
{
cout << "base operator=" << endl;
this->y = rf.y;
}
int y;
Base() : y(100) { }
};
class Derived : public Base
{
public:
int x;
Derived() : x(100) { }
};
int main()
{
Derived test;
Derived test2;
test2.x = 0;
test2.y = 0;
test.operator=(test2); // operator auto-generated for derived class but...
cout << test.x << endl << test.y << endl;
cin.ignore();
return 0;
}
程序输出:
> base operator=
> 0
> 0
现在我感到困惑的是: 该规则规定,派生类永远不会继承赋值运算符,而是创建自己的 operator=
,但在此示例中,基类的 operator=
在派生类上被调用。
其次,我能够在派生类上显式调用赋值运算符,而该运算符并未在派生类中显式定义。
现在,如果我理解正确的话,这意味着任何用户定义的基类运算符总是在派生类上被调用?
I'm trying to understand this behaviour but it seems I don't. Please see this code:
#include <iostream>
using namespace std;
class Base
{
public:
void operator=(const Base& rf)
{
cout << "base operator=" << endl;
this->y = rf.y;
}
int y;
Base() : y(100) { }
};
class Derived : public Base
{
public:
int x;
Derived() : x(100) { }
};
int main()
{
Derived test;
Derived test2;
test2.x = 0;
test2.y = 0;
test.operator=(test2); // operator auto-generated for derived class but...
cout << test.x << endl << test.y << endl;
cin.ignore();
return 0;
}
PROGRAM OUTPUT:
> base operator=
> 0
> 0
Now where I'm confused is:
The rule says that a derived class never inherits the assigment operator, instead it creates its own operator=
however in this example base's operator=
gets invoked on the derived class.
Second I was able to explicitly invoke an assigment operator on a derived class, which isn't in turn explicitly defined in the derived class.
Now if I understand it correctly, this means that any user defined base's operator always gets invoked on the derived class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
生成的会自动调用基类赋值运算符。
强制转换是为了避免意外调用模板化的
Base::operator=
像这样的:或者像这样的奇怪的:
如果您不这样做并且您的类允许它(即没有引用成员和一些其他晦涩的规则),编译器会自动声明一个赋值运算符,并且如果您实际使用,则另外定义它它在某个地方。
The generated ones automatically call the base-class assignment operator.
The cast is there to avoid an accidential call to a templated
Base::operator=
like this one:Or a strange one like this:
The compiler automatically declares an assignment operator if you do not and your class allows it (i.e., no reference members and some other arcane rules) and additionally defines it if you actually use it somewhere.
编译器生成的赋值运算符调用每个子对象的赋值运算符。这包括基类和非静态成员变量。
该标准规定(第 12.8 节
[class.copy]
)::
The compiler-generated assignment operator calls the assignment operator of each subobject. That includes base classes and non-static member variables.
The standard says (section 12.8
[class.copy]
):and
这是因为隐式定义的运算符 = 调用基类运算符 =。请参阅常见问题解答精简版:
我正在创建一个派生类;我的赋值运算符应该调用我的基类的赋值运算符吗?
That's because the implicitly defined operator = calls the base classes operator =. See the FAQ lite:
I'm creating a derived class; should my assignment operator call my base class's assignment operator?
4件事永远不会被继承
构造函数
复制构造函数
赋值运算符
析构函数
即使您没有编写赋值运算符,您的代码也会很好。
每当您使用赋值运算符时,编译器都会确保调用每个成员对象和基类的赋值运算符。
4 things never get inherited
Constructor
Copy-constructor
Assignment operator
Destructor
Even you have not written the assignment operator your code will be Woking fine.
Whenever you use assignment operator compiler will make sure that assignment operator for each member object and base class get called.