多載
- 多載=運算子
- 1先釋放舊物件資源
- 2用一個物件=給另外一個物件
- 3函式回傳值當左值 回傳一個參考
- 4 陣列類 Array& operator=(Array& a1);
- 5 字串類:MyString& operator=(const MyString& obj);
- char& operator[](int index) const;
- 運算子多載難點
- 多載=運算子
- 1先釋放舊物件資源
- 2用一個物件=給另外一個物件
- 3函式回傳值當左值 回傳一個參考
- 4 陣列類 Array& operator=(Array& a1);
- 5 字串類:MyString& operator=(const MyString& obj);
- char& operator[](int index) const;
- 多載[]陣列下標
- 1 回傳元素的參考,支持a[i]當左值 a[i]右值
- 2 陣列類 int& operator[](int i);
- 3 字串類 char& operator[](int index) const;
- && 和 || 不能被多載
- ()多載
- 多載=運算子
- 案例:陣列類 運算子多載優化
- 案例:字串類 運算子多載
等號運算子多載
- 多載=運算子
- 1 先釋放舊物件資源
- 2 用一個物件=給另外一個物件
- 3 函式回傳值當左值 回傳一個參考
- 4 陣列類 Array& operator=(Array& a1);
- 5 字串類:MyString& operator=(const MyString& obj);
- char& operator[](int index) const;
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
using namespace std;
class Student
{
public:
Student()
{
this->id = 0;
this->name = NULL;
}
Student(int id, char *name)
{
this->id = id;
//this->name = name;
int len = strlen(name);
this->name = new char[len + 1];
strcpy(this->name, name);
}
Student(const Student &another)
{
this->id = another.id;
//深拷貝
int len = strlen(another.name);
this->name = new char[len + 1];
strcpy(this->name, another.name);
}
Student & operator=(const Student &another)
{
//1 防止自身賦值
if (this == &another) {
return *this;
}
//2 先將自身的額外開辟的空間回收掉
if (this->name != NULL) {
delete[] this->name;
this->name = NULL;
this->id = 0;
}
//3 執行深拷貝
this->id = another.id;
int len = strlen(another.name);
this->name = new char[len + 1];
strcpy(this->name, another.name);
//4 回傳本身
return *this;
}
void printS()
{
cout << name << endl;
}
~Student() {
if (this->name != NULL) {
delete[] this->name;
this->name = NULL;
this->id = 0;
}
}
private:
int id;
char *name;
};
int main(void)
{
Student s1(1, "zhang3");
Student s2(s1);
s2 = s1;
Student s3(2, "li4");
//s2 = s3 = s1;//s2 = 賦值運算子
s1.printS();
s2.printS();
s3.printS();
return 0;
}
自定義陣列類運用多載
main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "MyArray.h"
using namespace std;
int main(void)
{
MyArray array1(10);//開辟10元素的陣列
//賦值操作
for (int i = 0; i < 10; i++) {
//array1.setData(i, i + 10);
array1[i] = i + 10;//space[1] = 1+10
}
cout << "--------" << endl;
cout << "array1:" << endl;
for (int i = 0; i < 10; i++) {
cout << array1[i] << " ";
}
cout << endl;
MyArray array2 = array1;
cout << "array2:" << endl;
for (int i = 0; i < array2.getLen(); i++) {
cout << array2[i] << " ";
}
cout << endl;
cout << " ------------" << endl;
MyArray array3(5);
cin >> array3;
cout << "array3:" << endl;
cout << array3 << endl;
cout << endl;
if (array3 != array1) {
cout << "不相等" << endl;
}
else {
cout << "相等 " << endl;
}
return 0;
}
Array.cpp
#include "MyArray.h"
MyArray::MyArray()
{
cout << "MyArray()..." << endl;
this->len = 0;
this->space = NULL;
}
MyArray::MyArray(int len)
{
if (len <= 0) {
this->len = 0;
return;
}
else {
this->len = len;
//給space開辟空間
this->space = new int[this->len];
cout << "MyArray::MyArray(int len) ..." << endl;
}
}
MyArray::MyArray(const MyArray &another)
{
if (another.len >= 0) {
this->len = another.len;
//深拷貝
this->space = new int[this->len];
for (int i = 0; i < this->len; i++) {
this->space[i] = another.space[i];
}
cout << "MyArray::MyArray(const MyArray &another) ..." << endl;
}
}
MyArray::~MyArray()
{
if (this->space != NULL) {
delete[]this->space;
this->space = NULL;
len = 0;
cout << "MyArray::~MyArray() ..." << endl;
}
}
void MyArray::setData(int index, int data)
{
if (this->space != NULL) {
this->space[index] = data;
}
}
int MyArray::getData(int index)
{
return this->space[index];
}
int MyArray::getLen() const
{
return this->len;
}
MyArray& MyArray::operator=(const MyArray& another)
{
if (this == &another) {
return *this;
}
if (this->space != NULL) {
delete[]this->space;
this->space = NULL;
this->len = 0;
}
if (another.len >= 0) {
this->len = another.len;
//深拷貝
this->space = new int[this->len];
for (int i = 0; i < this->len; i++) {
this->space[i] = another.space[i];
}
cout << "MyArray::operator=(const MyArray& another) ..." << endl;
}
return *this;
}
int & MyArray::operator[](int index) const
{
return this->space[index];
}
ostream &operator<<(ostream &os,const MyArray &array)
{
os << "遍歷整個陣列 " << endl;
//array.getLen(); //getLen(&array);
for (int i = 0; i < array.getLen(); i++) {
os << array[i] <<" ";//array.operator[]( i)
}
os << "呼叫的<<運算子多載" << endl;
return os;
}
istream &operator>>(istream &is, MyArray &array)
{
cout << "請輸入" << array.getLen() << "個數" << endl;
for (int i = 0; i < array.getLen(); i++) {
cin >> array[i];
}
return is;
}
bool operator==(MyArray &array1, MyArray &array2)
{
if (array1.len != array2.len) {
return false;
}
for (int i = 0; i < array1.len; i++) {
if (array1.space[i] != array2.space[i]) {
return false;
}
}
return true;
}
bool MyArray::operator!=(MyArray &another)
{
return !(*this == another);
}
Array.h
#pragma once
#include <iostream>
using namespace std;
class MyArray
{
public:
MyArray();
MyArray(int len);
MyArray(const MyArray &another);
~MyArray();
void setData(int index, int data);
int getData(int index);
int getLen() const ;
MyArray& operator=(const MyArray& another);
int & operator[](int index) const;
friend ostream &operator<<(ostream &os,const MyArray &array);
friend istream &operator>>(istream &is, MyArray &array);
friend bool operator==(MyArray &array1, MyArray &array2);
bool operator!=(MyArray &another);
private:
int len;
int *space;
};
多載小括號
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Sqr
{
public:
Sqr(int a) {
this->a = a;
}
int operator()(int value)
{
return value * value;
}
int operator()(int value1, int value2)
{
return value1 * value2;
}
private:
int a;
};
void func(int a)
{
}
int main(void)
{
Sqr s(10);
int value = https://www.cnblogs.com/ygjzs/p/s(2);
//s.operator()(2);
//將一個物件 當成一個普通函式來呼叫,
//稱這種物件是 仿函式,偽函式, 函式物件
cout << value << endl;
value = s(10, 20);
cout << value << endl;
return 0;
}
多載new和delete運算子
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;
class A
{
public:
A()
{
cout << "A()..." << endl;
}
A(int a) {
cout << "A(int)..." << endl;
this->a = a;
}
//多載的new運算子 依然會觸發物件的建構式
void * operator new(size_t size)
{
cout << "多載了new運算子" << endl;
return malloc(size);
}
void *operator new[](size_t size)
{
cout << "多載了new[]運算子" << endl;
return malloc(size);
}
void operator delete(void * p)
{
cout << "多載了delete運算子" << endl;
if (p != NULL) {
free(p);
p = NULL;
}
}
void operator delete[](void *p)
{
cout << "多載了delete[]運算子" << endl;
if (p != NULL) {
free(p);
p = NULL;
}
}
~A() {
cout << "~A().... " << endl;
}
private:
int a;
};
int main(void)
{
//char *array = malloc(sizeof(char)* 80);
//int *value_p = new int;
A *array_p = new A[10];
//array_p->operator new[](sizeof(A[10]));
delete[] array_p;
A *ap = new A(10);
//ap->operator new(sizeof(A));
delete ap;
return 0;
}
多載&&和||(不建議多載)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Test
{
public:
Test(int value) {
this->value = https://www.cnblogs.com/ygjzs/p/value;
}
Test operator+(Test &another)
{
cout << "執行了+運算子多載" << endl;
Test temp(this->value + another.value);
return temp;
}
bool operator&&(Test &another)
{
cout << "執行了&&運算子多載" << endl;
if (this->value && another.value) {
return true;
}
else {
return false;
}
}
bool operator||(Test &another)
{
cout << "多載了||運算子" << endl;
if (this->value || another.value) {
return true;
}
else {
return false;
}
}
~Test(){
cout << "~Test()..." << endl;
}
private:
int value;
};
int main(void)
{
int a = 1;
int b = 20;
Test t1(0);
Test t2(20);
//多載&&運算子,并不會發生短路現象,
if (t1 && (t1+t2) ) { //t1.operator&&( t1.operator+(t2) )
cout << "為真" << endl;
}
else {
cout << "為假" << endl;
}
cout << "------" << endl;
if (t1 || (t1 + t2)) {//t1.operator||( t1.operator+(t2) )
cout << "為真" << endl;
}
else {
cout << "為假" << endl;
}
return 0;
}
自定義智能指標
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <memory>
using namespace std;
class A
{
public:
A(int a)
{
cout << "A()..." << endl;
this->a = a;
}
void func() {
cout << "a = " << this->a << endl;
}
~A() {
cout << "~A()..." << endl;
}
private:
int a;
};
class MyAutoPtr
{
public:
MyAutoPtr(A * ptr)
{
this->ptr = ptr;//ptr = new A(10)
}
~MyAutoPtr() {
if (this->ptr != NULL) {
cout << "delte ptr" << endl;
delete ptr;
this->ptr = NULL;
}
}
A* operator->()
{
return this->ptr;
}
A& operator*()
{
return *ptr;
}
private:
A *ptr;
};
void test1()
{
#if 0
A* ap = new A(10);
ap->func();
(*ap).func();
delete ap;
auto_ptr<int> ptr(new int);
#endif
auto_ptr<A> ptr(new A(10));
ptr->func();
(*ptr).func();
}
void test2()
{
MyAutoPtr my_p(new A(10));
my_p->func(); //my_p.ptr -> func()
(*my_p).func(); // *ptr.func()
}
int main(void)
{
//test1();
test2();
return 0;
}
自定義字串類
main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include "MyString.h"
using namespace std;
int main(void)
{
string s1;
MyString s1("abc");
MyString s2("123");
//cout << s1 + s2 << endl;
cout << s1 << endl;
cout << s2 << endl;
#if 0
MyString s1("abc");
MyString s2(s1);
MyString s3 = "123";
cout << s1 << endl;
cout << s2 << endl;
s1[1] = 'x';
cout << s1 << endl;
s1 = s3;
cout << s1 << endl;
#endif
return 0;
}
String.cpp
#include "MyString.h"
MyString::MyString()
{
this->len = 0;
this->str =NULL;
}
MyString::MyString(const char *str)
{
if (str == NULL) {
this->len = 0;
this->str = new char[0 + 1];
strcpy(this->str, "");
}
else {
int len = strlen(str);
this->len = len;
this->str = new char[len + 1];
strcpy(this->str, str);
}
}
//初始化時候被呼叫的
MyString::MyString(const MyString &another)
{
this->len = another.len;
this->str = new char[this->len + 1];
strcpy(this->str, another.str);
}
MyString::~MyString()
{
if (this->str != NULL) {
cout << this->str << "執行了解構式" << endl;
delete this->str;
this->str = NULL;
this->len = 0;
}
}
char & MyString::operator[](int index)
{
return this->str[index];
}
MyString & MyString::operator=(const MyString &another)
{
if (this == &another) {
return *this;
}
if (this->str != NULL) {
delete[] this->str;
this->str = NULL;
this->len = 0;
}
this->len = another.len;
this->str = new char[this->len + 1];
strcpy(this->str, another.str);
return *this;
}
ostream & operator<<(ostream &os, MyString&s)
{
os << s.str;
return os;
}
istream & operator>>(istream &is, MyString &s)
{
//1 將s之前的字串釋放掉
if (s.str != NULL) {
delete[] s.str;
s.str = NULL;
s.len = 0;
}
//2 通過cin添加新的字串
char temp_str[4096] = { 0 };
cin >> temp_str;
int len = strlen(temp_str);
s.str = new char[len + 1];
strcpy(s.str, temp_str);
s.len = len;
return is;
}
MyString MyString::operator+(MyString &another)
{
MyString temp;
int len = this->len + another.len;
temp.len = len;
temp.str = new char[len + 1];
memset(temp.str, 0, len + 1);
strcat(temp.str, this->str);
strcat(temp.str, another.str);
return temp;
}
String.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class MyString
{
public:
MyString();
//MyString(int len); //創建一個長度是len的string物件
MyString(const char *str);
MyString(const MyString &another);
~MyString();
//多載運算子[]
char &operator[](int index);
//多載運算子>>
friend istream & operator>>(istream &is, MyString &s);
//多載=運算子
MyString & operator=(const MyString &another);
//多載==運算子
//多載!=運算子
//多載+運算子
MyString operator+(MyString &another);
//多載運算子<<
friend ostream & operator<<(ostream &os, MyString&s);
private:
int len;
char *str;
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/79223.html
標籤:C++
上一篇:c++-多載運算子(+-,++,--,+=,-=,cin,cout)
下一篇:c++-類與類的關系
