C++ (6) 重载/operator/enum class
来源:互联网 发布:mysql benchmark 使用 编辑:程序博客网 时间:2024/06/05 11:54
标签(空格分隔): cpp
(1) 函数重载
函数的名字相同,但是参数不同。可能是参数的个数不同,也可能是参数的类型不同。
以下例子,print就是重载函数
void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; }
(2)运算符重载
参考博客
operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=
整体上视为一个函数名。
在C++中,如果我们想比较两个整数,非常方便,用==就好了。但是如果我们要比较两个类对象,就不能直接用==了。要定义专门用于比较这两个类对象的==
号
定义重载运算符函数的时候,可以定义成类成员函数,也可以全局函数。
- 定义为类成员函数
class person{private: int age; public: person(int a){ this->age=a; } inline bool operator == (const person &ps) const; //定义};
实现方式如下:
inline bool person::operator==(const person &ps) const{ if (this->age==ps.age) return true; return false;}
调用方式如下:
int main(){ person p1(10); person p2(20); if(p1==p2) cout<<”the age is equal!”< return 0;}
- 定义为全局函数
enum class
参考博客
class为作用域
冒号后面的unsigned char是underlying type
默认枚举体的underlying type是int,
enum class ProducerProperty : unsigned char { AutoRepeat = 0, Flip, Rotation, Size, };
阅读全文
0 0
- C++ (6) 重载/operator/enum class
- C++operator重载问题
- [C++]operator--重载
- c++---operator重载++,--
- 奇怪的C++operator重载
- C++(7) 函数重载/operator
- 2.c++-重载运算符operator
- C++,重载输出操作符<<, cout.operator()
- 【C++】operator关键字(重载操作符)
- 重载Operator ++
- operator++ 重载
- operator << >>重载
- operator重载
- operator <<重载
- C/C++ enum class枚举类型
- C/C++——operator new 重载 & 单例模式
- enum 与enum class
- Enum Class
- CakePHP中的数据校验
- java线程模型
- Navigation Bar
- Noip 2012 同余方程
- java-split()
- C++ (6) 重载/operator/enum class
- 第八周项目3-稀疏矩阵的三元组表示的实现及应用(2)
- iOS上启动页 在模拟器上显示真机不显示
- TensorFlow和Caffe、MXNet、Keras等其他深度学习框架的对比
- Python—numpy做矩阵运算
- java 内部类 用法
- 开发类问题解决套路——5遍问题解决法
- python在cmd窗口显示彩色文字
- knockout获取当前点击DOM和当前行数据