//operator=必须是成员函数
class Person {
public:Person(int age);
public:Person();Person& operator=(const Person& p1);//类内成员函数重载“=”~Person();int *age;
};Person::Person(int age) {
this->age = new int(age);
}
Person::~Person() {
if (age != NULL) {
delete age;age = NULL;}
}Person::Person() {
this->age = new int(18);//默认18
}Person& Person::operator=(const Person& p1) {
//调用时this->operator=(p1)可简化为this=p1;/*if (this->age != NULL) {delete this->age;this->age = NULL;}this->age = new int(*p1.age);*/*this->age = *p1.age;//类自带operator=函数但是为浅拷贝,对于在堆区创建的数据需要深拷贝,需要对operator重写return *this;
}void test() {
Person person(18);Person p2(19);Person p3;cout << *p3.age << endl;p3= person=p2;cout << *p3.age << endl;cout << *person.age << endl;
}int main() {
test();
}
详细解决方案
operator=重载
热度:55 发布时间:2023-11-23 22:56:57.0
相关解决方案
- 请教:在s60 3rd中获得手机号、SIM卡号、IMSI、IMEI、CELL、AREA、COUNTRY、OPERATOR
- error a2028:operator expected,该如何解决
- friend ostream& operator<< <Type>(ostream& ,ListNode<Type>&);解决方案
- 哪位高手能告诉小弟我List<Operator>如何遍历
- 为什么执行不了报错 ORA-00920: invalid relational operator
- QMap<int,Item>iteratorerror C2593: “operator !=”不明确,该如何处理
- error C2665: “operator new” : 5个满载中没有一个可以转换参数1(从“const char [71]”类型)
- C++中自增操作符的重载:operator++()与operator(int)
- java中运行时异常 the operator(什么什么)is undefinded for the argument byte[s] 的原因之一
- unary operator expected解决方法
- copy-assignment operator
- Prometheus Operator 使用operator管理alertmanager
- Prometheus Operator 使用ServiceMonitor管理监控配置
- Prometheus Operator 介绍 安装
- Kotlin-14-运算符重载(operator)
- 错误解析 error:unable to find numeric literal operator ‘operator““a/b/c/...‘
- python setup.py install 遇到 SyntaxError: operator not allowed in environment markers
- gdb Could not find operator[]
- 【深入浅出flink】第3篇:多图讲解flink核心概念(通俗易懂)Job、Task、SubTask、Slot、Slotsharing、Parallelism、Operator Chains
- set关联容器去除重复地图点出现问题 error: no match for ‘operator<’ (operand types are ‘const Point’ and ‘const Point
- operator=重载
- 进阶之路:从零到一在k8s上部署高可用prometheus —— prometheus-operator
- 再谈operator new operator delete
- 基础语法.operator
- C++中 operator 的两种使用方式
- 四,运算符(Operator)
- Uncaught Error: [vuex] store must be called with the new operator
- kubernetes中一键部署prometheus operator
- MySQL Operator 01 | 架构设计概览
- 容器化 | ClickHouse Operator 原理解析