重载
- 重载=操作符
- 1先释放旧对象资源
- 2用一个对象=给另外一个对象
- 3函数返回值当左值 返回一个引用
- 4 数组类 array& operator=(array& a1);
- 5 字符串类:mystring& operator=(const mystring& obj);
- char& operator const;
- 运算符重载难点
- 重载=操作符
- 1先释放旧对象资源
- 2用一个对象=给另外一个对象
- 3函数返回值当左值 返回一个引用
- 4 数组类 array& operator=(array& a1);
- 5 字符串类:mystring& operator=(const mystring& obj);
- char& operator const;
- 重载[]数组下标
- 1 返回元素的引用,支持a[i]当左值 a[i]右值
- 2 数组类 int& operator;
- 3 字符串类 char& operator const;
- && 和 || 不能被重载
- ()重载
- 重载=操作符
- 案例:数组类 运算符重载优化
- 案例:字符串类 运算符重载
等号运算符重载
- 重载=操作符
- 1 先释放旧对象资源
- 2 用一个对象=给另外一个对象
- 3 函数返回值当左值 返回一个引用
- 4 数组类 array& operator=(array& a1);
- 5 字符串类:mystring& operator=(const mystring& obj);
- char& operator 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 = 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 = 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; };
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/600636.html