c++笔记2-操作符重载
来源:互联网 发布:网络摄像机 要多少电压 编辑:程序博客网 时间:2024/06/12 19:59
操作符重载:
操作符重载为操作符提供不同的语义
1.通过operator关键字可以利用函数扩展操作符,operator的本质是通过函数重载实现操作符重载
例1::扩展“+”,使他可以支持复数相加
#include <cstdlib>
#include <iostream>
using namespace std;
struct Complex
{
int a;
int b;
};
Complex operator+(const Complex& c1, const Complex& c2)
{
Complex ret;
ret.a = c1.a + c2.a;
ret.b = c1.b + c2.b;
return ret;
}
int main(int argc, char *argv[])
{
Complex c1 = {1, 2};
Complex c2 = {3, 4};
Complex c3 = c1+c2;
cout<<"c3.a = "<<c3.a<<endl;
cout<<"c3.b = "<<c3.b<<endl;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
2.用operator关键字扩展的操作符可以用于类:
private声明使得类的成员不能被外界访问,但是通过friend关键字可以例外的开放权限
例二:
#include <cstdlib>
#include <iostream>
using namespace std;
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
friend Complex operator+ (const Complex& c1, const Complex& c2);
};
Complex operator+ (const Complex& c1, const Complex& c2)
{
Complex ret;
ret.a = c1.a + c2.a;
ret.b = c1.b + c2.b;
return ret;
}
int main(int argc, char *argv[])
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
例三:重载“<<”
#include <cstdlib>
#include <iostream>
using namespace std;
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
friend Complex operator+ (const Complex& c1, const Complex& c2);
friend ostream& operator<< (ostream& out, const Complex& c);
};
ostream& operator<< (ostream& out, const Complex& c)
{
out<<c.a<<" + "<<c.b<<"i";
return out;
}
Complex operator+ (const Complex& c1, const Complex& c2)
{
Complex ret;
ret.a = c1.a + c2.a;
ret.b = c1.b + c2.b;
return ret;
}
int main(int argc, char *argv[])
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2;
cout<<c1<<endl;
cout<<c2<<endl;
cout<<c3<<endl;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
3.用成员函数重载的操作符,比全局操作符重载函数少一个参数,即左操作数,不需要使用friend关键字
例4:
#include <cstdlib>
#include <iostream>
using namespace std;
class Complex
{
int a;
int b;
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
Complex operator+ (const Complex& c2);
friend ostream& operator<< (ostream& out, const Complex& c);
};
ostream& operator<< (ostream& out, const Complex& c)
{
out<<c.a<<" + "<<c.b<<"i";
return out;
}
Complex Complex::operator+ (const Complex& c2)
{
Complex ret(0, 0);
ret.a = this->a + c2.a;
ret.b = this->b + c2.b;
return ret;
}
int main(int argc, char *argv[])
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2;
cout<<c1<<endl;
cout<<c2<<endl;
cout<<c3<<endl;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
4.什么时候使用全局函数重载操作符?什么时候使用成员函数重载操作符?
当无法修改左操作数的类时,使用全局函数进行重载
=, [], ()和->操作符只能通过成员函数进行重载
5.不要重载&&和||操作符
&&和||是C++中非常特殊的操作符
&&和||内置实现了短路规则
操作符重载是靠函数重载来完成的
操作数作为函数参数传递
C++的函数参数都会被求值,无法实现短路规则
6.++操作符的重载
++操作符只有一个操作数
++操作符有前缀和有后缀的区分,区分前后缀:C++中通过一个占位参数来区分前置运算和后置运算
例:
obj++:
Complex operator++(int)
{
Complex ret = *this;
a++;
b++;
return ret;
}
++obj:
Complex& operator++()
{
++a;
++b;
return *this;
}
- C++【操作符重载】
- c++::操作符重载
- C++:操作符重载
- 操作符重载——C/C++学习笔记
- (C/C++学习笔记操作符重载入门)
- C++(笔记)重载运算符简单合并操作
- c++笔记2-操作符重载
- 操作符重载(C++)
- C++-重载操作符<< >> = + - * /
- c++_操作符重载
- C++重载操作符笔记
- c++操作符重载笔记
- 【笔记-C++】 运算符重载
- C/C++: 操作符重载案例
- C++库研究笔记——赋值操作符operator=的正确重载方式(2)
- 操作符重载 2
- (C/C++学习笔记)操作符重载的两种方法
- C++操作符重载专题(C++)
- JavaScript整理2
- API学习Arrays
- 按位运算
- JQuerry简介
- JAVA迭代器与ArrayList集合的使用
- c++笔记2-操作符重载
- [来源未知][树形dp]逐个击破
- API学习Collection
- 一些密码学中的概念收集
- OSG动画库Animation解析(二)
- JS学习第二天
- 2017 Multi-University Training Contest
- 20170725 用Jquery修改CSS
- 自举电路、自举电容