共计 2604 个字符,预计需要花费 7 分钟才能阅读完成。
导读 | 继友元知识过后,就到了今天的 C ++ 运算符重载的内容了,运算符重载是 C ++ 里比较重要的内容。这篇博文不会一下子讲完各种运算符重载,因为太多了了也不好吸收掌握,所以运算符重载我准备分多次记录和分享,那么接下来进入正文 |
1. 类外运算符重载
class Point { | |
private: | |
int x,y; | |
public: | |
// 系统 C ++ 源码,大量使用此方式 :x(x), y(y) | |
Point(int x, int y) :x(x), y(y) {} | |
// set get 函数 | |
void setX(int x) {this->x = x;} | |
void setY(int y) {this->y = y;} | |
int getX() {return this->x;} | |
int getY() {return this->y;} | |
}; | |
/* 类外运算符重载 | |
* 在真实开发过程中,基本上都是写在类的里面的,外部是不能获取内部的私有成员的 | |
* */ | |
Point operator + (Point point1,Point point2){int x = point1.getX() + point2.getX(); | |
int y = point1.getY() + point2.getY(); | |
Point res(x, y); | |
return res; | |
} | |
int main(){Point pointA(10,20); | |
Point pointB(10,20); | |
Point pointC=pointA+pointB; | |
cout | |
日志输出:20 , 40 | |
两个对象做 + 法运算就是执行了运算符重载函数 | |
2. 类内部运算符号重载 | |
class Point { | |
private: | |
int x,y; | |
public: | |
Point(){} | |
// 系统 C ++ 源码,大量使用此方式 :x(x), y(y) | |
Point(int x, int y) :x(x), y(y) {} | |
// set get 函数 | |
void setX(int x) {this->x = x;} | |
void setY(int y) {this->y = y;} | |
int getX() {return this->x;} | |
int getY() {return this->y;} | |
/* | |
* 常量引用:不允许修改,只读模式 | |
* & 性能的提高,如果没有 & 运行 + 构建新的副本,会浪费性能 | |
* 如果增加了 & 引用是给这块内存空间取一个别名而已 | |
* */ | |
Point operator + (const Point & point){ | |
int x=this->x+point.x; | |
int y=this->y+point.y; | |
return Point(x,y); | |
} | |
Point operator - (const Point & point){ | |
int x=this->x-point.x; | |
int y=this->y-point.y; | |
return Point(x,y); | |
} | |
void operator ++() { // ++ 对象 | |
this->x = this->x + 1; | |
this->y = this->y + 1; | |
} | |
void operator ++ (int) { // 对象 ++ | |
this->x = this->x + 1; | |
this->y = this->y + 1; | |
} | |
/* 重载 > (istream & _START, Point & point) { | |
// 接收用户的输入,把输入的信息 | |
_START >> point.x >> point.y; | |
return _START; | |
} | |
}; | |
int main(){Point pointA(30,50); | |
Point pointB(10,20); | |
// Point pointC=pointA-pointB; | |
++pointA; | |
// cout > pointC; // >> 是我们自己重载的哦 | |
cout | |
类内部运算符重载,允许访问私有变量 | |
传入的参数是常量引用,const 表示不可更改,& 可以提升性能,只会有一个变量别名,不加会拷贝一份,浪费内存。 | |
> 重载,需要加 friend 友元函数来进行重载 | |
ostream & _START:表示输出 | |
istream & _START:表示输入 | |
3.[] 运算符号重载 | |
class ArrayClass { | |
private: | |
int size =0 ; // 大小 开发过程中,给 size 赋默认值,不然可能会出现,无穷大的问题 | |
int * arrayValue; // 数组存放 int 类型的很多值 | |
public: | |
ArrayClass(){ | |
/* 指针类型必须分配空间 */ | |
arrayValue= static_cast(malloc(sizeof(int *) * 10)); | |
} | |
void set(int index, int value) {arrayValue[index] = value; // [] 目前不是我的 | |
size+=1; | |
} | |
int getSize() { // size 成员的目标:是为了循环可以遍历 | |
return this->size; | |
} | |
// 运算符重载 [index] | |
int operator[](int index) {return this->arrayValue[index]; // 系统的 | |
} | |
}; | |
// 输出容器的内容 | |
void printfArryClass(ArrayClass arrayClass) {cout | |
4.c++ 继承 | |
class Person { | |
public: | |
char *name; | |
int age; | |
public: | |
Person(char *name, int age) : name(name) { | |
this->age = age; | |
cout name age | |
默认是 隐式代码:: private Person | |
私有继承:在子类里面是可以访问父类的成员,但是在类的外面不行 | |
必须公开继承,才可以访问父类的成员 | |
先执行父类的构造函数,再执行子类的构造函数 | |
5. 多继承 | |
class BaseActivity1 { | |
public: | |
void onCreate() {cout | |
c++ 允许多继承,可能会出现二义性,原则上是尽量避免二义性 | |
通过明确父类的方式解决二义性 | |
通过子类重写父类的方法规避二义性 | |
6. 通过虚继承来解决二义性问题 | |
// 祖父类 | |
class Object{ | |
public: | |
int number; | |
void show() {cout | |
如果没有虚继承,那么 son 对象访问 number 就会报二义性的问题,同时访问 show 方法同样存在二义性问题 | |
由于在继承的时候添加了虚继承,就能解决类似这样的问题,虚继承的含义是:将通过继承得来的 number 和 show 方法,放置在另外一个统一空间上,这样子类再访问的时候就不会出现二义性的问题了。 | |
到此这篇关于 C ++ 运算符重载与多继承及二义性详解的文章就介绍到这了 | |
阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配 | |
腾讯云新客低至 82 元 / 年,老客户 99 元 / 年 | |
代金券:在阿里云专用满减优惠券 | |
正文完
星哥玩云-微信公众号
