运算符 = 覆盖 c++
Operator = override c++
我正在尝试覆盖我的=运算符。我有 2 个指针父级,他们需要是指针。
P1* 和 P2*
这是我在父级内部的覆盖
Parent Parent::operator=(const Parent& otherParent) const
{
return Parent(otherParent);
}
这是我的复制构造函数:
Parent::Parent(const Parent& otherParent) : myChild("") {
this->name = otherParent.GetParentName();
myChild = Child(otherParent.GetChild());
}
这是我的主要:
#include <iostream>
#include "Parent.h"
#include "Child.h"
using namespace std;
int main() {
Child c1 = Child("rupert");
Parent* p1 = new Parent("Parent1", c1);
cout << *p1 << endl;
Child c2 = Child("bob");
Parent* p3 = new Parent("Parent3", c2);
cout << *p3 << endl;
*p3 = *p1;
cout << *p3 << endl;
cin.get();
return 0;
}
为什么打印出这个:
父名:父1 子名:鲁珀特
父名称:父3 子名称:鲍勃
父名称:父3 子名称:鲍勃
第三次打印应与第一次相同。
将赋值语句*p3 = *p1
视为如下工作:
p3->operator=(*p1);
这是有效的语法。如果你这样看,你应该能够看到赋值运算符需要修改this
如果它实际上要做任何赋值。现在,您创建并返回一个立即销毁的临时。
相关文章:
- 使用ios:ate写入到流会覆盖现有文件
- 我可以重新分配/覆盖std::字符串吗
- 叮叮当当在修复时插入多个"覆盖"说明符
- 谷歌模拟和覆盖关键字
- TMap::Emplace() 在应用现有密钥时会覆盖吗?
- 此测试()中发生了什么意外过程?为什么总是覆盖 ch[0 1 2..]?
- 使用子类覆盖基类中定义的函数
- 当覆盖存在时调用基本虚拟"binded to object"函数
- C++指针在 for 循环中被覆盖
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 为什么我的全局 new() 覆盖被绕过了?
- 被覆盖的函数不会反映在基类中,这是正常行为吗?
- C++找出覆盖同一行的数组
- 专门化模板覆盖函数/避免对象切片
- 重写打印函数而不是覆盖基类
- C++中"覆盖功能的异常规范比基本版本更宽松"的奇怪错误
- 如何在 c++ 中覆盖相同的文本文件
- 覆盖应用程序的低级别键盘挂钩问题
- glBindTexture 是否覆盖活动纹理单元的内容
- "虚拟""覆盖"析构函数