本文主要是介绍Linux C++ 017-运算符重载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Linux C++ 017-运算符重载
本节关键字:Linux、C++、运算符重载、匿名函数
相关库函数:
运算符重载的概念
对已有的运算符重新进行定义,赋予其另外一种功能,以适应不同的数据类型* 运算符重载可以发生函数重载
* 对于内置的数据类型的表达式的运算符是不可能改变的
* 不要滥用运算符重载
运算符重载的作用
(1) 加号运算符(operator+):实现两个自定义数据类型相加的运算(2) 左移运算符(operator<<):可以输出自定义数据类型(3) 递增运算符(operator++):实现自己的整型数据递增* 前置递增返回的是引用* 后置递增返回的是值(4) 赋值运算符(operator=):C++编译器至少给一个类添加4个函数:1.默认构造函数2.默认析构函数3.默认拷贝函数4.赋值运算符 operator= ,对属性进行值拷贝* 如果类中有属性指向堆区,做赋值操作时会出现深浅拷贝问题(5) 关系运算符(operator> operator< operator>= operator<= operator== operator!=):可以让两个自定义类型对象进行对比操作(6) 函数调用运算符(operator()):1.函数调用运算符 () 也可以重载2.由于重载后使用的方式非常像函数的调用,因此成为仿函数3.仿函数没有固定写法,非常灵活
加号运算符重载
class Person
{
public: Person(int a, int b);
public:Person operator+(Person &p);int m_A;int m_B;
};
Person::Person(int a, int b)
{m_A = a;m_B = b;
}
//通过成员函数重载运算符:
Person 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;
左移运算符重载
class Person
{friend ostream operator<<(ostream &cout, Person &p);
public: Person();Person operator<<(Person &p);private: int m_A;int m_B;
};
Person::Person(int a, int b)
{m_A = a;m_B = b;
}
//不会利用成员函数重载左移运算符,因为无法实现cout在左边
//通过全局函数重载运算符:
ostream operator<<(ostream &cout, Person &p)
{cout << "m_A = " << p.m_A << "m_B = " << p.m_B;return cout;
}
void test()
{Person p(10,10);cout << p << "hello" << endl;
}
递增运算符重载
class MyInteger
{friend ostream operator<<(ostream cout, MyInteger myint);
public:MyInteger();//前置MyInteger& operator++();//后置MyInteger& operator++(int);//int 为占位参数,可以区分前置和后置递增
private:int m_Num;
};
MyInteger::MyInteger()
{m_Num = 0;
}
//通过成员函数实现后置递增
MyInteger& MyInteger::operator++()
{m_Num++;return *this;
}
//通过成员函数实现前置递增
MyInteger MyInteger::operator++(int)
{//先记录当时的结果MyInteger temp = *this;//递增m_Num++;//最后将记录的结果返回return temp;
}
//通过全局函数重载左移运算符
ostream operator<<(ostream cout, MyInteger myint)
{cout << myint.m_Num;return cout;
}
void test()
{MyInteger myint;cout ++(++myint) << endl;cout << myint << endl;
}
赋值运算符重载
class Person
{
public:Person(int age);~Person();Person& operator=(Person &p);
private:int *m_Age;
};
Person::Person(int age)
{m_Age = new int(age);
}
Person::~Person()
{if(m_Age != NULL){delete m_Age;m_Age = NULL;}
}
Person& Person::operator=(Person &p)
{//应该先判断是否有属性在堆区,若有要先释放干净,然后深拷贝if(m_Age != NULL){delete m_Age;m_Age = NULL;}//深拷贝m_Age = new int(*p.m_Age);return *this;
}void test()
{Person p1(18);Person p2(20);p2 = p1;cout << "p1的年龄为:" << *p1.m_Age << endl;cout << "p1的年龄为:" << *p2.m_Age << endl;
}
关系运算符重载
class Person
{
public:Person(string name, int age);bool operator==(Person& p);bool operator!=(Person& p);
private:strig m_Name;int m_Age;
};
Person::Person(string name, int age)
{m_Name = name;m_Age = age;
}
bool Person::operator==(Person& p)
{if(this->m_Name == p.m_Name && this->m_Age == p.m_Age){return true;}return false;
}
bool Person::operator!=(Person& p)
{if(this->m_Name != p.m_Name || this->m_Age != p.m_Age){return true;}return false;
}void test()
{Person p1("TOm", 18);Person p2("Tom", 18);if(p1 == p2){cout << "p1 和 p2 相等" << endl;}else{cout << "p1 和 p2 不相等" << endl;}if(p1 != p2){cout << "p1 和 p2 不相等" << endl;}else{cout << "p1 和 p2 相等" << endl;}
}
函数调用运算符重载
class MyPrint
{
public:MyPrint();void operator()(string test);
private:};
MyPrint::MyPrint()
{}
void MyPrint::operator()(string test)
{cout << test << endl;
}
void MyPrint02(string test)
{cout << test << endl;
}
void test()
{MyPrint myp;myp.MyPrint("hello");MyPrint02("你好");
}
匿名函数
class MyAdd
{
public:int operator()(int num1, int num2);
};
int MyAdd::operator()(int num1, int num2)
{return num1+num2;
}
void test2()
{MyAdd myadd;int ret = myadd(100,100);cout << "ret = " << ret << endl;//匿名函数对象cout << MyAdd()(100, 100) << endl;
}
这篇关于Linux C++ 017-运算符重载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!