运算符的虚拟超定义=
Virtual surdefinition of operator =
我有两个类,我想对运算符=进行surdefine。
class Composant {
string description;
...
virtual const Composant& operator=(const Composant &c)
{
description = c.description;
return *this;
}
}
另一个继承了:
class Ecran : Composant {
int format, pitch;
double taille;
...
const Ecran& operator=(const Ecran &e)
{
format = e.format;
taille = e.taille;
pitch = e.pitch;
//traiter composant
Composant::operator=(e);
}
主要代码示例:
Composant *p=new Ecran(.....);
Composant *t=new Ecran(.....);
(*p)=*t;
这是正确的方法吗?
我假设我们必须将运算符=的surdefinition放在虚拟中,每次我们都有这种类型的继承,那么?运算符==也是如此。。。
这是正确的方法吗?
没有虚拟调度进入:
(*p)=*t;
您只是在呼叫Composant::operator=
。这是否是正确的方法取决于你想要实现的目标。
可以定义虚拟operator=
,但不推荐使用。
const Ecran& operator=(const Ecran &e)
{
format = e.format;
taille = e.taille;
pitch = e.pitch;
//traiter composant
Composant::operator=(e);
}
必须返回值
return *this;
(*p)=*t;
调用Composant
类的operator=
而不是Ecran
类,因为这在Composant
上操作。您可以定义
virtual const Composant& operator=(const Composant &c);
在Ecran
类中,它将被调用,但它将无法访问运算符右侧的Ecran
成员。
相关文章:
- C++无法定义虚拟函数 OUTER 类和头文件
- 如何进行故障排除:未定义对"非虚拟 thunk to ..."的引用
- 重载虚拟行为是否定义良好?
- 纯虚拟类和错误未定义对 'vtable 的引用
- 使(虚拟)函数在大多数派生类中无法访问中间基类中可访问,定义良好?
- 虚拟成员函数的定义是否强制在同一转换单元中动态初始化静态数据成员?
- 基类可以声明虚拟方法但不定义它吗?仍然在派生类中定义
- 无法使用在子类中定义的虚拟getter实现基类
- 即使基类和派生类只使用基元数据类型,我是否需要定义虚拟析构函数
- 没有针对完全专用模板类的外联虚拟方法定义
- 为什么虚拟方法生成对_sbrk的未定义引用?
- 在C++为矢量编写自定义虚拟分配器时遇到问题
- 是否需要定义虚拟析构函数
- 此处未定义虚拟析构函数
- 如何在子类中初始化具有自定义虚拟函数的基成员变量
- 在C++中定义虚拟get和set函数是否被认为是一种好的做法
- 在派生类中重新定义虚拟运算符==
- 在C++中定义虚拟继承的两种不同方式
- 在哪里可以定义虚拟函数
- 为什么不能使用来定义虚拟函数