1.在具有指針的類中,必須具有拷貝函數(shù)、拷貝賦值函數(shù)、析構(gòu)函數(shù)這三種內(nèi)容,其實(shí)現(xiàn)形式如下:
#ifndef _MYSTRING_#define _MYSTRING_#includeclass String{
private:
char* m_data;
public:
String(const char* cstr=0); //default構(gòu)造函數(shù)
String(const String& str); //拷貝構(gòu)造函數(shù)
String& operator=(const String&str); //拷貝賦值函數(shù)
~String(); //析構(gòu)函數(shù)
char* get_c_str()const{return m_data;}
};
inline String::String(const char* cstr){???
if(cstr){
m_data=new char[strlen(cstr)+1];
strcpy(m_data,cstr);
}
else{
m_data=new char[1];
*m_data='\0';
}
}
inline String::~String(){
delete[]m_data;
}
inline String::String(const String& str){
delete[]m_data;
m_data=new char[strlen(str.m_data)+1];
strcpy(m_data,str.m_data);
}
inline String& String::operator=(const String& str){
if(this==&str)return *this;
delete[]m_data;?????? //self assignment
m_data=new char[strlen(str.m_data)+1];
strcpy(m_data,str.m_data);
return *this;
}
#endif
C++中申請(qǐng)和釋放動(dòng)態(tài)內(nèi)存相對(duì)應(yīng)的操作符為new delete,new先分配內(nèi)存空間再實(shí)施構(gòu)造函數(shù),delete先實(shí)施析構(gòu)函數(shù)再釋放內(nèi)存空間。
self assighment的作用:當(dāng)左右指針指向同一個(gè)內(nèi)存空間時(shí),若此時(shí)沒(méi)有self assighment
,則會(huì)產(chǎn)生不確定行為。
2.全局輸出函數(shù)構(gòu)造:
#include<iostream>
ostream& operator<<(ostream& os,const String& str){
os<<str.get_c_str():
return os;}
3.內(nèi)存空間模式有stack和heap兩種。其中stack是存在于某一作用域的一塊內(nèi)存空間,當(dāng)你調(diào)用函數(shù)時(shí),就會(huì)生成一個(gè)stack空間來(lái)存放接受的參數(shù)及返回地址。heap是由操作系統(tǒng)提供的一塊全局內(nèi)存空間,new操作符申請(qǐng)到的內(nèi)存空間就是已heap方式分配而來(lái)的。
stack內(nèi)存空間在函數(shù)結(jié)束時(shí)會(huì)自動(dòng)清除,即stack的生命期在作用域結(jié)束后自動(dòng)清除,而heap內(nèi)存空間在他被delete之前會(huì)一直存在,為了避免出現(xiàn)內(nèi)存泄漏,在函數(shù)中由new申請(qǐng)的動(dòng)態(tài)內(nèi)存要在函數(shù)結(jié)束時(shí)使用delete釋放掉。
如想使stack object具有全局的生命期,可以在對(duì)象前加上static。
面對(duì)字符串申請(qǐng)內(nèi)存空間時(shí),要申請(qǐng)字符串長(zhǎng)度+1的空間。(空字符串要由一個(gè)空間來(lái)存放結(jié)束符號(hào)‘\0’)
complex*pc=new complex(1,2);
編譯器轉(zhuǎn)化為:
void*element=operator new(sizeof(complex));
pc=static_cast<complex*>(element);
pc->complex::comliex(1,2);
delete pc;
編譯器轉(zhuǎn)化為:
complex::~complex(pc);
operator delete(pc);
系統(tǒng)一次從heap中申請(qǐng)到的內(nèi)存空間必為8n字節(jié)
4.array new 需要由array delete來(lái)清除
String*p=new String[3];
delete[]p//喚起三次dtor
String* p=new String[3];
delete p;//喚起1次dtor,兩份內(nèi)存沒(méi)有析構(gòu),內(nèi)存泄露
int*a=new int[3];
delete a;//可行,對(duì)象的類型時(shí)內(nèi)置類型或者是無(wú)自定義析構(gòu)函數(shù)的類型
5.static 靜態(tài)函數(shù)只能對(duì)靜態(tài)數(shù)據(jù)進(jìn)行處理
構(gòu)造函數(shù)在private區(qū)的類:singleton
class singleton{
public:
static A& getIn();
setup(){...}
private:
A();
A(const A& a);};
inline A& A::getIn(){
static A a;
return a;}
6.類模板和函數(shù)模板
class template:
template<typename T>
class A{
private:
T re,im;friend A& _doapl(A*,const A&);
public:
A(T a=0,T b=0):re(a),im(b){};
A&operator+=(const A&);
T real()const{return re;}
T imag()const{return im;}};
function template
template<typename T>
inline T& min(const T& a,const T& b){
return a<b?a:b}//編譯器會(huì)對(duì)函數(shù)進(jìn)行引數(shù)推導(dǎo),調(diào)用相應(yīng)對(duì)象中的重載函數(shù)
7.三種類于類的關(guān)系
Composition:
template<typename T>
class A{
....
protected:
B<T> b;//底層容器
public:
bool empty()const{return b.empty();}//利用b的操作函數(shù)完成
};
A::A(..):B(){..}? A的構(gòu)造函數(shù)首先調(diào)用B的default構(gòu)造函數(shù),由內(nèi)而外
A::~A(...){...~B()} A的析構(gòu)函數(shù)先調(diào)用,然后才調(diào)用B的析構(gòu)函數(shù) 由外而內(nèi)
Delegation:Composition by reference
Inheritance:
class A;
class B:public A{.....};
構(gòu)造由內(nèi)而外,析構(gòu)由外而內(nèi)。
父類與子類同時(shí)具有的函數(shù)必須為虛函數(shù)以便該函數(shù)在子類中可以重新構(gòu)造
virtual function:
class A{ virtual x() const=0;//純虛
virtual y();//虛}