C++学习记录3——友元与运算符重载
一、友元
程序中的一些私有属性,也想让类外的一些特殊的函数或类进行访问,友元的目的就是让函数或类访问另一个类内的私有成员。
关键字friend
友元的三种实现:
1.全局函数做友元
class Building
{
//写在类的前端
friend void godgay(Building* building);
public:
Building()
{
m_sittingroom = "客厅";
m_bedroom = "卧室";
}
string m_sittingroom;
private:
string m_bedroom;
};
void godgay(Building*building)
{
cout << "访问" << building->m_sittingroom << endl;//可正常访问
cout << "访问" << building->m_bedroom<< endl;//声明友元后才可
}
int main()
{
Building building;
godgay(&building);
return 0;
}
2.类做友元
class Building
{
friend class GodGay; //类做友元
public:
Building();
string m_sittingroom;
private:
string m_bedroom;
};
class GodGay
{
public:
GodGay();
void visit(); //访问Building中的属性
Building* building; //building为指针
};
//类外写成员函数,注意作用域
Building::Building()
{
m_sittingroom = "客厅";
m_bedroom = "卧室";
}
GodGay::GodGay()
{
//在堆区创建建筑物对象,并用指针维护
building = new Building;
}
void GodGay::visit()
{
cout << "访问" << building->m_sittingroom << endl;
cout << "访问" << building->m_bedroom << endl;//声明友元后才可
}
int main()
{
GodGay gg;
gg.visit();
return 0;
}
3.成员函数做友元
这里注意!!!:
Building类开头会先介绍自己的朋友,所以友元类要先于Building类定义,而GodGay类中又有Building* building,所以要在GodGay类前先声明Building类,而在GodGay类后再定义。GodGay类中visit函数也要写在类外
另外:
前向声明Building类让编译器知道有此类存在,而大小未知无法分配内存,因此前向声明后只能使用类的对象指针或对象引用(指针的内存是固定的)Building* building 若改为Building p会报错
class Building;
class GodGay
{
public:
GodGay();
void visit(); //访问Building中的私有属性
void visit2(); //不可访问Building中的私有属性
Building* building;
};
class Building
{
friend void GodGay::visit(); //成员函数做友元
public:
Building();
string m_sittingroom;
private:
string m_bedroom;
};
//类外写成员函数,注意作用域
Building::Building()
{
m_sittingroom = "客厅";
m_bedroom = "卧室";
}
GodGay::GodGay()
{
//在堆区创建建筑物对象,并用指针维护
building = new Building;
}
void GodGay::visit()
{
cout << "访问" << building->m_sittingroom << endl;
cout << "访问" << building->m_bedroom << endl;//声明友元后才可
}
void GodGay::visit2()
{
cout << "访问" << building->m_sittingroom << endl;
//cout << "访问" << building->m_bedroom << endl;//不可
}
int main()
{
GodGay gg;
gg.visit();
gg.visit2();
return 0;
}
二、运算符重载
概念:对已有的运算符重新定义,赋予其另一种功能,以适应不同的数据类型。
重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。
注意:
对内置的数据类型的表达式的运算符是不可能改变的。
不要滥用运算符重载。(如不要重载“+”实现减法)
1.加号运算符重载
作用:实现两个自定义数据类型的相加运算
要实现如上Person类的相加,可写成员函数实现
Person PersonAdd(Person &p)
{
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
通过成员函数重载“+”实现
Person operator+(Person &p)
{
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
调用时本质为:Person p3 = p1.operator+(p2);
可简化为:Person p3 = p1 + p2;
通过全局函数重载“+”
Person operator+(Person &p1, Person &p2)
{
Person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
调用时本质为:Person p3 = operator+(p1,p2);
可简化为:Person p3 = p1 + p2;
配合函数重载可实现Person + int:
函数重载:链接:C++学习记录1函数重载
Person operator+(Person &p1, Person &p2)
{
Person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
Person operator+(Person &p1, int num) //改为int,函数重载
{
Person temp;
temp.m_A = p1.m_A + num;
temp.m_B = p1.m_B + num;
return temp;
}
2.左移运算符重载
作用:实现输出自定义数据类型
一般不用成员函数重载左移运算符,因为无法实现cout在左侧的常规形式
本质为:p.operator<<(cout);
因为要由对象p调用,所以p在左侧
简化为:p << cout;
只能通过全局函数重载:
//ostream标准输出流类,是cout对应的类型
ostream & operator<<(ostream &out,Person &p) //此处引用给cout起别名out,所以下面的cout都可用out,效果相同
{
cout<<"m_A="<<p.m_A<<"m_B="<<p.m_B<<endl;
return cout; //仍返回cout保证可链式追加<<输出
}
可用cout << p << endl;
直接输出输出p对象(Person p中的p.m_A和p.m_B)
使用时若访问私有对象可配合使用友元
3.递增运算符重载
作用:可以实现自己的整形数据
占位参数:链接:C++学习记录1占位参数
class Myinteger
{
friend ostream& operator<<(ostream& out,const Myinteger &myint);
public:
Myinteger()
{
m_Num = 0;
}
//重载前自增,返回引用是为了一直对一个数据自增,实现++(++myint)
Myinteger& operator++()
{
m_Num++; //先++运算
return *this; //再返回自身
}
//重载后自增,返回值,若返回引用,返回的是局部对象temp的引用,
//当前函数执行完后会被释放,导致错误
Myinteger operator++(int) //此处int是占位参数,用于区分前后自增
{
//先记录当时结果
Myinteger temp = *this;
//后,递增
m_Num++;
//最后返回记录结果
return temp;
}
private:
int m_Num;
};
//注意:因为实现重载后自增时,返回的是值,
//所以此时参数myint加const或使用值传递不用引用
ostream& operator<<(ostream& out, const Myinteger &myint)
{
cout << myint.m_Num;
return cout;
}
int main()
{
Myinteger myint;
cout << ++myint << endl;
cout << myint << endl;
cout << ++(++myint) << endl;
cout << myint << endl;
cout << myint++ << endl;
cout << myint << endl;
//因temp被释放,所以此处不能链式后自增
cout << (myint++)++<< endl;
cout << myint << endl; //此行代码运行,发现结果只加1
return 0;
}
PS:
- 前自增返回引用,后自增返回值,所以后自增/自减更耗时
- 默认的int类型就无法使用
(a++)++
,后自增在表达式中只能有一个且必须在最外层,如++((++a)++)
会报错(只要后自增就有临时temp) - 也可以将temp在堆区开辟空间,就可以在后自增时使用引用传递
//改为用引用传递
Myinteger& operator++(int) //此处int是占位参数,用于区分前后自增
{
//先记录当时结果
//Myinteger temp = *this;
Myinteger* temp;
temp = new Myinteger;
*temp = *this;
//后,递增
m_Num++;
//最后返回记录结果
return *temp;
}
private:
int m_Num;
};
ostream& operator<<(ostream& out, Myinteger &myint)//这里也用引用
{
cout << myint.m_Num;
return cout;
}
4.赋值运算符重载
C++编译器至少给一个类添加四个函数
- 默认构造函数
- 默认析构函数
- 默认拷贝构造函数,对属性值进行拷贝
- 赋值运算符 operator=(这是函数),对属性值进行拷贝
编译器默认的赋值运算符为浅拷贝,可能引发问题
class Person
{
public:
Person(int age)
{
m_age = new int(age);
}
~Person()
{
if (m_age!=NULL) //不重载,使用赋值会出错,重复释放空间
{
delete m_age;
m_age = NULL;
}
}
//重载赋值运算符
Person& operator=(Person& p)
{
//编译器默认提供浅拷贝:m_age = p.m_age
//应先判断是否有属性在堆区,有则释放再深拷贝
if (m_age!=NULL)
{
delete m_age;
m_age = NULL;
}
m_age = new int(*p.m_age);
return *this;
}
private:
int *m_age;
};
int main()
{
Person p1(18);
Person p2(20);
p2 = p1;
Person p3 = p2 = p1; //返回自身可实现连等
return 0;
}
5.关系运算符重载
作用:让两个自定义类型进行对比操作
class Person
{
public:
Person(string name, int age)
{
m_Name = name;
m_Age = age;
}
bool operator==(Person& p)
{
if (this->m_Name ==m_Name&&this->m_Age ==m_Age)
{
return true;
}
return false;
}
bool operator!=(Person& p)
{
if (this->m_Name == m_Name && this->m_Age == m_Age)
{
return false;
}
return true;
}
private:
string m_Name;
int m_Age;
};
int main()
{
Person p1("Tom", 18);
Person p2("Tom", 18);
if (p1==p2)
{
cout << "p1=p2" << endl;
}
return 0;
}
6.函数调用运算符重载
函数调用运算符 () 也可重载
重载后使用的方式非常像函数调用,因此称为仿函数
仿函数没有固定写法,非常灵活
class Myprint
{
public:
void operator()(string test)
{
cout << test << endl;
}
};
int main()
{
Myprint myprint; //创建对象后再调用
myprint("hello world"); //输出字符串
Myprint()("what f"); //匿名函数对象 类型()为匿名对象
return 0;
}