+、-、*、/、++、--、==、!=、* ->、&&、||...
對(duì)于內(nèi)置數(shù)據(jù)類型,編譯器知道如何做運(yùn)算,編譯器不知道如何讓兩個(gè)類進(jìn)行運(yùn)算
- 如果向讓自定義數(shù)據(jù)類型 進(jìn)行+法運(yùn)算,就需要重載+運(yùn)算符
- 在成員函數(shù)或者全局函數(shù)里 ,重寫一個(gè)+法運(yùn)算符的函數(shù)
- 函數(shù)名 operator+(){}
- 運(yùn)算符重載也可以提供多個(gè)版本
加法運(yùn)算符
類名+operator+(){};
例:
成員函數(shù)
class Person
{
public:
Person()
{}
Person(int a, int b) :a(a), b(b)
{}
Person operator+(Person& p)
{
Person tmp;
tmp.a = this->a + p.a;
tmp.b = this->b + p.b;
return tmp;
}
int a;
int b;
};
void test()
{
Person p1(1, 20);
Person p2(3, 34);
Person p3 = p2 + p1;
cout << p3.a << " " << p3.b << endl;
}
例:
全局函數(shù)
class Person
{
public:
Person()
{}
Person(int a, int b) :a(a), b(b)
{}
int a;
int b;
};
Person operator+(Person &p1,Person& p2)
{
Person tmp;
tmp.a = p1.a + p2.a;
tmp.b = p1.b + p2.b;
return tmp;
}
void test()
{
Person p1(1, 20);
Person p2(3, 34);
Person p3 = p2 + p1;
cout << p3.a << " " << p3.b << endl;
}
左移運(yùn)算符重載
- 不要隨意亂用符號(hào)重載
- 內(nèi)置數(shù)據(jù)類型的運(yùn)算符不可以重載
- cou<<直接對(duì)Person自定義數(shù)據(jù)類型 進(jìn)行輸出
- 寫到全局函數(shù)中 ostream& operator<<(ostream& cout,Person &p){}
- 如果重載時(shí)候想訪問p的私有成員,那么全局函數(shù)要做person的友元函數(shù)
例:
class Person
{
//友元函數(shù)
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person()
{}
Person(int a, int b) :a(a), b(b)
{}
private:
int a;
int b;
};
//<<重載
ostream& operator<<(ostream& cout, Person& p)
{
cout << "a=" << p.a << "b=" << p.b;
return cout;
}
void test()
{
Person p(23, 54);
cout << p << endl;
}
++運(yùn)算符重載
重載++運(yùn)算符 operator++()前置 operator++(int)后置
前置理念:先++,后返回自身
后置理念:先保存住原值,內(nèi)部++,返回臨時(shí)數(shù)據(jù)
例:
class MyInteter
{
friend ostream& operator<<(ostream &cout, MyInteter myInteter);
public:
MyInteter()
{
num = 0;
};
//前置++重載
MyInteter& operator++()
{
this->num++;
return *this;
};
//后置++重載 tmp是臨時(shí)數(shù)據(jù),數(shù)據(jù)結(jié)束會(huì)銷毀,所以要返回值
MyInteter operator++(int)
{
MyInteter tmp=*this;
num++;
return tmp;
}
private:
int num;
};
//此處參數(shù)如果是引用,后置++返回的數(shù)據(jù)已經(jīng)銷毀了,會(huì)導(dǎo)致錯(cuò)誤,因此要傳值
ostream& operator<<(ostream& cout, MyInteter myInt)
{
cout << myInt.num;
return cout;
};
void test()
{
MyInteter my;
cout << ++my << endl;
cout <<my++<< endl;
cout << my << endl;
}
指針運(yùn)算符重載(智能指針)
用來托管自定義類型的對(duì)象,讓對(duì)象進(jìn)行自動(dòng)的釋放。
有了智能指針,讓智能指針托管這個(gè)person對(duì)象,對(duì)象的釋放就不用操心了,讓智能指針管理。
為了讓智能指針向普通的Person*指針一樣使用,就要重寫->和*
例:
class Person
{
public:
Person()
{
}
Person(int age)
{
this->age = age;
}
~Person()
{
cout << "Person析構(gòu)了" << endl;
}
void showAge()
{
cout << "年齡為:" << this->age << endl;
}
int age;
};
class smartPointer
{
public:
smartPointer()
{
}
smartPointer(Person* p)
{
this->person = p;
}
//重載指針
Person* operator->()
{
return this->person;
}
//重載*
Person& operator*()
{
return *person;
}
~smartPointer()
{
cout << "智能指針析構(gòu)了" << endl;
if (this->person)
{
delete this->person;
this->person = NULL;
}
}
private:
Person* person;
};
void test()
{
smartPointer sn(new Person(10));
sn->showAge();
(*sn).showAge();
}
賦值運(yùn)算符重載
系統(tǒng)默認(rèn)給類提供的賦值運(yùn)算符寫法是簡單值拷貝,所以要重載=運(yùn)算符
如果想鏈?zhǔn)骄幊?,就要重?運(yùn)算符
class Person
{
public:
Person()
{
}
Person(char* name)
{//開辟新空間
this->name =new char[strlen(name)+1];
//拷貝數(shù)據(jù)
strcpy(this->name, name);
}
~Person()
{
//判斷是否為空,不為空釋放
if (this->name)
{
delete[] this->name;
this->name = NULL;
}
}
//重載=號(hào)運(yùn)算符
Person& operator=(Person &p)
{
//判斷是否為空,不為空釋放
if (this->name)
{
delete[] this->name;
this->name = NULL;
}
//開辟新空間
this->name = new char[strlen(p.name) + 1];
拷貝數(shù)據(jù)
strcpy(this->name,p.name);
return *this;
}
char* name;
};
void test()
{
Person p1((char*)"小紅");
Person p2((char*)"小花");
p1 = p2;
cout << p1.name << endl;
}
[]運(yùn)算符重載
返回?cái)?shù)組索引的引用
int& operator[](int index)
pAddress是數(shù)組指針
return this->pAddress[index]