【C++-入门语法(五)】教程文章相关的互联网学习教程文章

C++-入门语法(四)【代码】

运算符重载(operator overload)运算符重载(操作符重载):可以为运算符增加一些新的功能#include <iostream> #include "Point.h" using namespace std;// operator overload//class Point { // friend Point operator+(const Point &, const Point &); // int m_x; // int m_y; //public: // Point(int x, int y) :m_x(x), m_y(y) { } // void display() { // cout << "(" << this->m_x << ", " << this->m_y << ")" << endl; ...

C++-入门语法(五)【代码】

仿函数(函数对象)仿函数:将一个对象当作一个函数一样来使用 对比普通函数,它作为对象可以保存状态#include <iostream> using namespace std;//int sum(int a, int b) { // return a + b; //}class Sum {int m_age; public:Sum(int age) :m_age(age) { }int operator()(int a, int b) {if (this->m_age > 10) {}else {}return a + b;} };class Point {friend ostream &operator<<(ostream &, const Point &); public:int m_x;in...