是不是所有的运算符都可以重载
不是
不可以重载的运算符
1. . 成员访问运算符
2. ., -> 成员指针访问运算符
3. :: 域运算符
4. sizeof 长度运算符
5. ?: 三目运算符
6. # 预处理符
但是 new/delet new[] delete[] 可以的
// 运算符重载
// 可以使得用户自定义的数据,使用起来更简单
/*
* java String "abc" + "def"
* c/c++ strcat
*/
class Complex{
public:
Complex(int a=0,int i=0){
this->a = a;
this->i = i;
}
void print(){
cout <<"Complex: ("<<a<<"+"<<i<<")"<<endl;
}
private:
friend Complex myAdd(Complex c1, Complex c2);
friend Complex operator+(Complex c1,Complex c2);
int a;
int i;
};
Complex myAdd(Complex c1, Complex c2){
Complex tmp(c1.a+c2.a,c1.i +c2.i);
return tmp;
}
//void operator-(Complex i){
//
//}
Complex operator+(Complex c1,Complex c2){//看成是一个函数
Complex tmp(c1.a+c2.a,c1.i +c2.i);
return tmp;
}
int main(){
//TODO: 运算符重载
cout<<"运算符重载"<<endl;
int a = 0;
int b = 0;
int c;
c = a + b;//基础数据类型 如何运算 C++编译器是不是已经定义好了的
//"abc " + "def"
//复数 实数 + 虚数 a + i 加法 (a1+i1) + (a2+i2) = (a1+a2) + (i1+i2)
Complex c1(1,2);
c1.print();
Complex c2(3,4);
Complex sum;// sum c1 + c2;C++编译器如何支持操作符重载的?
// sum = myAdd(c1,c2);
// sum.print();
sum = c1 + c2;// 函数调用
sum.print();
//是不是所有的运算符都可以重载
//不是
/*不可以重载的运算符
* 1. . 成员访问运算符
* 2. .*, ->* 成员指针访问运算符
* 3. :: 域运算符
* 4. sizeof 长度运算符
* 5. ?: 三目运算符
* 6. # 预处理符
*/
// new/delet new[] delete[] 可以的
return 0;
}
重载demo
运算符重载分类
按照实现方式
- 运算符重载为成员函数
- 运算符重载为全局函数(友元)
运算符 需要的操作数 - 一元运算符 ++ -- 前置/后置
- 二元运算符 数学运算符(+ - * /)
ObjectL op ObjectR
重载为成员函数
ObjectL.operator op(ObjectR)
重载为友元函数
operator op(ObjectL,ObjectR)
//1. 友元函数
//当我们无法修改左操作数的类时,只能使用友元函数
//2. 成员函数
// = [] () -> 操作符 只能通过成员函数进行重载
重载运算符的步骤
sum c1 + c2
c1.operator +(c2);
- 把操作符重载 认为是一个函数调用 -> operator +
推断出函数原型 - 分析函数参数 根据左右操作数的个数 -> operator + (ClassName &classname)
- 分析函数的返回值
友元 Complex operator+(Complex &c);
#include <iostream>
using namespace std;
class Complex {
public:
// Complex(){}
Complex(int a = 0, int i = 0) : a(a), i(i) {}
Complex operator-(Complex &c) {
Complex tmp(this->a - c.a, this->i - c.i);
return tmp;
}
Complex& operator--() {
this->a--;
this->i--;
return *this;
}
Complex& operator--(int) {
Complex tmp = *this;
this->a--;
this->i--;
return tmp;
}
void printf() {
cout << "complex:(" << a << "+" << i << "i)" << endl;
}
private:
friend Complex myadd(Complex c1, Complex c2);
friend Complex operator+(Complex c1, Complex c2);
friend Complex &operator++(Complex &c);
friend Complex &operator++(Complex &c, int);
friend ostream& operator<<(ostream &out,Complex &c);
int a;
int i;
};
Complex myadd(Complex c1, Complex c2) {
Complex tmp(c1.a + c2.a, c1.i + c2.i);
return tmp;
}
Complex operator+(Complex c1, Complex c2) {
Complex tmp(c1.a + c2.a, c1.i + c2.i);
return tmp;
}
Complex &operator++(Complex &c) {
c.a++;
c.i++;
return c;
}
//后置++
Complex &operator++(Complex &c, int) {
Complex tmp = c;
c.a++;
c.i++;
return tmp;
}
ostream& operator<<(ostream &out,Complex &c) {
out << "Complex:(" << c.a << "+" << c.i << "i)" << endl;
return out;
}
int main() {
Complex c1(1, 2);
Complex c2(3, 4);
c1.printf();
c2.printf();
Complex sum = c1 + c2;
Complex add = myadd(c1, c2);
sum.printf();
add.printf();
Complex cut;
cut = c1 - c2;
cut.printf();
++cut;
cut.printf();
--cut;
cut.printf();
cut--;
cut.printf();
cut++;
cut.printf();
cout<<"==========="<<endl;
cout<<cut<<"---"<<endl;
return 0;
}
只能用成员函数重载
例子
class Name{
protected:
char *pName;
int size;
public:
Name(char *pName) : pName(pName) {
size = strlen(pName);
this->pName = (char *)malloc(sizeof(size + 1));
strcpy(this->pName,pName);
}
Name(const Name& name){
size = name.size;
this->pName = (char *)malloc(sizeof(size + 1));
strcpy(this->pName,pName);
}
// 只能是成员函数重载,无法使用友元函数
Name& operator = (Name& obj){
if(this->pName != NULL){
delete[] pName;
size = 0;
}
size = obj.size;
pName = new char[size + 1];
strcpy(pName,obj.pName);
Name tmp(pName);
return *this;
}
virtual ~Name() {
if(pName != NULL) {
free(pName);
pName = nullptr;
size = 0;
}
}
};
void objtest(){
Name obj1("abcdef");
Name obj2 = obj1;
//Name& operateor = (Name& obj)
}
int main() {
objtest();
return 0;
}
重载实例 [] == =
-> header.h
#ifndef TEMPC_MYARRAY_H
#define TEMPC_MYARRAY_H
class MyArray{
public:
MyArray(int size);
MyArray(const MyArray &array);
~MyArray();
void setData(int index,int value);
int getData(int index);
int arraySize();
/**
* 重载【】
* @param i
* @return
*/
int& operator[](int i);
// MyArray a[10]
//int b = a[];/a[2] = 3;
// int& operator[](int i);
/**
* 重载=
* @param array
* @return
*/
MyArray& operator=(MyArray &array);
// operator=(),如何用 MyArray a[10],a1 a1 =a
// operator=(MyArray &array) MyArray array 原始 MyArray &array 拷贝
// MyArray& operator=(MyArray &array) 返回值 返回它本身
/**
* 重载==
* @param array
* @return
*/
bool operator==(MyArray &array);
private:
int size;
int *m_space;
};
#endif //TEMPC_MYARRAY_H
-> cpp
#include <iostream>
#include "MyArray.h"
#include <stdlib.h>
using namespace std;
MyArray::MyArray(int size){
this->size = size;
m_space = (int*)malloc(sizeof(int) * size);
}
MyArray::MyArray(const MyArray &array){
this->size = array.size;
m_space = array.m_space;
}
MyArray::~MyArray(){
if(m_space != NULL) {
free(m_space);
}
}
void MyArray::setData(int index,int value){
m_space[index] = value;
}
int MyArray::getData(int index){
return m_space[index];
}
int MyArray::arraySize(){
return size;
}
int &MyArray::operator[](int i) {
return m_space[i];
}
MyArray& MyArray::operator=(MyArray &array){
if(m_space != nullptr){
delete[] m_space;
size = 0;
}
size = array.size;
m_space = new int[size];
for (int i = 0; i < size; ++i) {
m_space[i] = array[i];
}
return *this;
}
bool MyArray::operator==(MyArray &array) {
if(this->size != array.size){
return false;
}
for (int i = 0; i < size; ++i) {
if(this->m_space[i] != array[i]){
return false;
}
}
return true;
}
int main() {
MyArray arr(10);
MyArray arr2(10);
for (int i = 0; i < arr.arraySize(); ++i) {
// arr.setData(i,i);
arr[i] = i *2;
}
arr2 = arr;
cout << arr.getData(9) << (arr2 == arr) << endl;
return 0;
}
网友评论