c 自定義智慧型指標和string類

2021-08-20 16:56:05 字數 3690 閱讀 4785

main.cpp

#include "mystring.h"

//智慧型指標的意義是你new乙個空間,是指標指向這個空間,不用在呼叫delete去**指標指向的空間,系統會自己幫我們**

int main()

if (s1 != s2)//過載了!=操作符

//int a, b;

//cin >> a >> b;//cin也可以連續輸入

/*char *c = null;

char *d = null;

strcpy(c, d);//不可以對指向為空的指標進行拷貝

*/char *c = new char[3];

const char *d = "111";

char e[3];

char f[3] = ;

//strlen()函式是遇'\0'結束的,在new來的空間時,因為最後一位不是'\0'因此strlen()函式仍會繼續向下相加,直到遇到第乙個'\0'為止才停止計算,因此用strlen計算陣列是不準確的,最好只用來計算字串的長度

cout << "strlen(c)=" << strlen(c) << endl;//結果是未知的

cout << "strlen(d)=" << strlen(d) << endl;//結果是3

cout << "strlen(e)=" << strlen(e) << endl;//結果是未知的

cout << "sizeof(c)=" << sizeof(c) << endl;//結果是0,因為陣列內都是'\0',strlen()函式遇'\0'結束

//strcpy()與strlen()不同,strcpy()函式在不是字串的時候就會停止,而strlen()只會在遇到'\0'才停止

mystring m1;

mystring m2("xuhaoxuan");

mystring m3;

mystring m4("xuqiang ");

m3 = m1 + m2;

cout << "111=" << m3;

cout << "222=" << m4 + m2;

return 0;

mystring.cpp

#include "mystring.h"

mystring::mystring()

mystring::mystring(int len)

else

}mystring::mystring(const char *str)

else

}mystring::mystring(const mystring &s)

else

}mystring &mystring::operator=(const mystring &s)//型別不用加作用域,只有類裡的成員方法才需要加類名作用域,記住operator+等這些操作符過載函式也是成員方法,一定需要帶上類名作用域

else

if (this->str != null)

this->str = new char[len];

strcpy(this->str, s.str);

}return *this;

}char &mystring::operator(int index)

bool mystring::operator==(const mystring &s)

else

}bool mystring::operator!=(const mystring &s)

else

}char *mystring::getstr()const

mystring mystring::operator+(const mystring &s)

if (this->str == null && s.str != null)

if (this->str != null && s.str == null)

if (this->str != null && s.str != null)

point++;

}strcpy(point, s.str);

return temp;

}return temp;

}mystring::~mystring()

}ostream &operator<<(ostream &out, const mystring &s)

else

}istream &operator>>(istream &in, const mystring &s)

//下面三段**的寫法是錯誤的,在返回引用的時候,變數a就可以改變,但這時a在test()函式中是不可改變的,這兩個相衝突,所以在函式中被const修的變數是絕對不可以返回引用的

/*int &test(const int a)

*/mystring.h

#pragma once

#define _crt_secure_no_warnings

#include

#include //為系統自帶的智慧型指標標頭檔案,是用智慧型指標需要呼叫這個標頭檔案

#include //為系統自帶的字串類,string類裡重寫了很多操作符

using namespace std;

class mystring

;//new開闢出來的記憶體空間如果沒有被delete,則將會在程式執行結束時作業系統自己**,並不是在重啟電腦時釋放,網上很多講解都是錯誤的!!!

class test

void function()

//乙個物件在棧空間時,當程序結束時會自動呼叫析構函式**這個物件

//乙個物件在堆空間時,你不使用delete去顯式的釋放這個物件,那麼當程序結束時,不會呼叫析構函式,因此凡是new出來的物件,都需要手動的delete

//在堆區間開闢出來的物件只有析構函式是不主動觸發的,只有呼叫delete刪除這個物件空間時,才會觸發析構函式

//注意這兒的delete與析構函式內部的delete完全不同,析構函式內部的delete是為了釋放有類內部指標開闢的堆空間,而外部的delete是為了釋放new出來的物件,但使用智慧型指標開闢物件,則就不需要手動釋放了

~test()

private:

int a;

};class myautoptr

test *operator->()

test operator*()

~myautoptr()

}private:

test * ptr;

};class student

private:

int id;

string name;//string這個類即為char *的封裝類,可以直接指向字串,裡面還重寫了很多操作符,使字串操作更簡單便捷

};class test1

test1(const test1 &t)//const看右邊,只看一位,是誰就修飾誰,&看左邊,可以看多為,是誰就修飾誰

int geta()

test1 &operator=(const test1 &t)

private:

int a;

};class test2

private:

test1 t;

int a;

};istream &operator>>(istream &in, const mystring &s);

ostream &operator<<(ostream &out, const mystring &s);

c 自定義string類

1.標頭檔案部分 define crt secure no warnings pragma once include includeusing namespace std class mystring 2.函式定義部分 include mystring.h mystring mystring mys...

c 智慧型指標

auto prt 它是 它所指向物件的擁有者 所以當自身物件被摧毀時候,該物件也將遭受摧毀,要求乙個物件只有乙個擁有者,注意 auto prt 不能使用new 來分配物件給他 include include using namespace std template void bad print au...

c 智慧型指標

很久沒寫部落格了,不知道如何表達了,哈哈.我先介紹一下深淺拷貝.class copy 此時a.ptr和b.ptr指向同乙個物件,當我們delete a.ptr時 b.ptr所指向的物件已經不存在了,要是我們引用b.ptr指向的物件也就會出問題了.深拷貝 把a.ptr所指向的物件拷貝乙份給b.ptr ...