智能指针
用途:用来托管堆区创建的对象的释放
#include<iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
this->m_age = age;
cout << "Person的有参函数构造" << endl;
}
void showage()
{
cout << "年龄为:" << this->m_age << endl;
}
~Person()
{
cout << "Person的析构函数调用" << endl;
}
int m_age;
};
// 智能指针 用来托管new出来的对象的释放
class SmartPointer
{
public:
SmartPointer(Person *p)
{
cout << "SmartPointer有参构造函数调用" << endl;
this->person = p;
}
// 重载指针运算符
Person * operator->()
{
return this->person;
}
Person & operator*()
{
return *(this->person);
}
Person * person;
~SmartPointer()
{
if (this->person != NULL)
{
cout << "SmartPointer析构函数调用" << endl;
delete this->person;
this->person = NULL;
}
}
};
void test01()
{
// Person *p = new Person(10);
// delete p;
SmartPointer sp = SmartPointer(new Person(18));
sp->showage(); // sp->->showage() 编译器简化为 sp->showage;
(*sp).showage();
}
int main()
{
test01();
system("pause");
return 0;
}
赋值运算符重载
系统会默认给一个类添加4个函数:默认构造、拷贝构造、析构、operator=
由于系统提供的operator= 会进行简单的值拷贝,导致如果属性中有堆区的数据,就会进行重复释放 解决方案 需要重载 operator=
#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
Person(){}
Person(const char *name, int age)
{
this->m_name = new char[strlen(name) + 1];
strcpy_s(this->m_name, strlen(name) + 1, name);
this->m_age = age;
}
// 系统会默认给一个类 创建至少3个函数 默认构造 有参构造 拷贝构造(简单值拷贝) operator=(值拷贝)
Person(const Person &p)
{
this->m_name = new char[strlen(p.m_name) + 1];
strcpy_s(this->m_name, strlen(p.m_name) + 1, p.m_name);
this->m_age = p.m_age;
}
Person & operator=(const Person &p)
{
if (this->m_name != NULL)
{
delete[]this->m_name;
this->m_name = NULL;
}
this->m_name = new char[strlen(p.m_name) + 1];
strcpy_s(this->m_name, strlen(p.m_name) + 1, p.m_name);
this->m_age = p.m_age;
return *this;
}
~Person()
{
// cout << "析构" << endl;
if (this->m_name != NULL)
{
delete[] this->m_name;
this->m_name = NULL;
}
}
public:
char *m_name;
int m_age;
};
void test01()
{
Person p1("Tom",18);
Person p2("Jerry", 19);
p1 = p2;
cout << "p1的姓名:" << p1.m_name << " p1的年龄:" << p1.m_age << endl;
cout << "p2的姓名:" << p2.m_name << " p2的年龄:" << p2.m_age << endl;
}
void test02()
{
/*int a = 10;
int b = 20;
int c ;
c = a = b ;
cout << "a = " << a << " b = " << b << " c = " << c << endl;*/
Person p1("Tom", 18);
Person p2("Jerry", 19);
Person p3;
p3 = p1 = p2;
cout << "p1的姓名:" << p1.m_name << " p1的年龄:" << p1.m_age << endl;
cout << "p2的姓名:" << p2.m_name << " p2的年龄:" << p2.m_age << endl;
cout << "p3的姓名:" << p3.m_name << " p3的年龄:" << p3.m_age << endl;
Person p4(p3);
cout << "p4的姓名:" << p4.m_name << " p4的年龄:" << p4.m_age << endl;
}
int main()
{
// test01();
test02();
system("pause");
return 0;
}
关系运算符重载
#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
bool operator==(const Person &p)
{
/*if (this->m_name == p.m_name&&this->m_age == p.m_age)
{
return true;
}
else
{
return false;
}*/
return this->m_name == p.m_name&&this->m_age == p.m_age;
}
bool operator!=(const Person &p)
{
return !(this->m_name == p.m_name&&this->m_age == p.m_age);
}
string m_name;
int m_age;
};
void test01()
{
int a = 10;
int b = 20;
if (a == b)
{
cout << "a等于b" << endl;
}
else
{
cout << "a不等于b" << endl;
}
}
void test02()
{
Person p1("Tom",18);
Person p2("Tom",18);
if (p1 == p2)
{
cout << "p1和p2相等" << endl;
}
else
{
cout << "p1和p2不相等" << endl;
}
}
int main()
{
// test01();
test02();
system("pause");
return 0;
}
函数调用运算符重载
#include<iostream>
#include<string>
using namespace std;
class MyFunc
{
public:
void operator()(string text)
{
cout << text << endl;
}
};
class MyAdd
{
public:
int operator()(int a, int b)
{
return a + b;
}
protected:
private:
};
void text01()
{
MyFunc myfunc;
myfunc("hello,world!");
}
void text02()
{
cout << MyAdd()(10, 10) << endl;
}
int main()
{
// text01();
text02();
system("pause");
return 0;
}
逻辑运算符
不要重载与和或
符号重载总结
-
= [ ] ()和->操作符只能通过成员函数进行重载
-
<< 和>>只能同过全局函数配合友元函数进行重载
-
不要重载 && 和 || 操作符