运算符重载3
来源:互联网 发布:广告优化师 编辑:程序博客网 时间:2024/06/06 03:17
#include<iostream>
using namespace std;
#include<string>
struct date{
int year;
int month;
int day;
};
struct Person{
string name;
int age;
bool gender;
double salary;
date birth;
Person(){cout<<"创建person对象在:"<<this<<endl;age = 2;}
~Person(){cout<<"释放person对象在:"<<this<<endl;}
};
class autoptr{
Person *p;
static int cnt;
public:
autoptr(Person *p):p(p){}
autoptr(const autoptr&a):p(a.p){++cnt;}
~autoptr(){cout<<cnt<<":"<<endl;if(0 == --cnt)delete p;}
Person *operator->(){return p;}
Person& operator*(){return *p;}
};
int autoptr::cnt = 0;//静态数据成员初始化要在外面
int main()
{
autoptr a = new Person;
autoptr b = a;//释放会产生段错误
autoptr c = a;
autoptr d = a;
autoptr e = a;
cout<<a->age<<endl;
a->name = "hehe";//a.operator->()->name
cout<<(*b).name<<endl;
(*c).birth.year = 1991;
cout<<d->birth.year <<endl;
}
using namespace std;
#include<string>
struct date{
int year;
int month;
int day;
};
struct Person{
string name;
int age;
bool gender;
double salary;
date birth;
Person(){cout<<"创建person对象在:"<<this<<endl;age = 2;}
~Person(){cout<<"释放person对象在:"<<this<<endl;}
};
class autoptr{
Person *p;
static int cnt;
public:
autoptr(Person *p):p(p){}
autoptr(const autoptr&a):p(a.p){++cnt;}
~autoptr(){cout<<cnt<<":"<<endl;if(0 == --cnt)delete p;}
Person *operator->(){return p;}
Person& operator*(){return *p;}
};
int autoptr::cnt = 0;//静态数据成员初始化要在外面
int main()
{
autoptr a = new Person;
autoptr b = a;//释放会产生段错误
autoptr c = a;
autoptr d = a;
autoptr e = a;
cout<<a->age<<endl;
a->name = "hehe";//a.operator->()->name
cout<<(*b).name<<endl;
(*c).birth.year = 1991;
cout<<d->birth.year <<endl;
}
0 0
- 运算符重载3
- 运算符重载3
- 运算符重载3
- 运算符重载 [++,--,+,<<; &&,||(不可重载)]
- 重载之运算符重载
- 9-3分数类 运算符重载
- 复数类中的运算符重载 3
- 复数类--重载运算符3+
- 复数类--重载运算符3+
- 复数类--重载运算符3+
- 复数类重载运算符3+
- 复数类中的运算符重载3
- OJ-复数类-重载运算符3+
- oj--复数类--重载运算符3+
- C++primer学习:重载运算符(3)
- 复数运算符重载(3)
- 分数运算符重载(3)
- 运算符重载的函数(3)
- 命令行操作记录
- 在外部存储(SD卡)读写文件及检测SD卡状态详解
- 运算符重载2
- 网络编程3---常用服务器编程模型
- TCP/IP、Http、Socket的区别以及面试中关于网络协议常问的考点
- 运算符重载3
- android 内存优化
- canvas and bitmap 比较不错的文章分享下
- python中的reduce介绍:
- ProjectOne(04) - 番外篇02 requirejs初体验
- HashSet和TreeSet
- 1041. 考试座位号
- Activity绑定自定义视图
- HDU 1017 A Mathematical Curiosity