1.在具有指針的類中,必須具有拷貝函數(shù)、拷貝賦值函數(shù)、析構(gòu)函數(shù)這三種內(nèi)容,其實現(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++中申請和釋放動態(tài)內(nèi)存相對應(yīng)的操作符為new delete,new先分配內(nèi)存空間再實施構(gòu)造函數(shù),delete先實施析構(gòu)函數(shù)再釋放內(nèi)存空間。
self assighment的作用:當左右指針指向同一個內(nèi)存空間時,若此時沒有self assighment
,則會產(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)存空間,當你調(diào)用函數(shù)時,就會生成一個stack空間來存放接受的參數(shù)及返回地址。heap是由操作系統(tǒng)提供的一塊全局內(nèi)存空間,new操作符申請到的內(nèi)存空間就是已heap方式分配而來的。
stack內(nèi)存空間在函數(shù)結(jié)束時會自動清除,即stack的生命期在作用域結(jié)束后自動清除,而heap內(nèi)存空間在他被delete之前會一直存在,為了避免出現(xiàn)內(nèi)存泄漏,在函數(shù)中由new申請的動態(tài)內(nèi)存要在函數(shù)結(jié)束時使用delete釋放掉。
如想使stack object具有全局的生命期,可以在對象前加上static。
面對字符串申請內(nèi)存空間時,要申請字符串長度+1的空間。(空字符串要由一個空間來存放結(jié)束符號‘\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中申請到的內(nèi)存空間必為8n字節(jié)
4.array new 需要由array delete來清除
String*p=new String[3];
delete[]p//喚起三次dtor
String* p=new String[3];
delete p;//喚起1次dtor,兩份內(nèi)存沒有析構(gòu),內(nèi)存泄露
int*a=new int[3];
delete a;//可行,對象的類型時內(nèi)置類型或者是無自定義析構(gòu)函數(shù)的類型
5.static 靜態(tài)函數(shù)只能對靜態(tài)數(shù)據(jù)進行處理
構(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}//編譯器會對函數(shù)進行引數(shù)推導(dǎo),調(diào)用相應(yīng)對象中的重載函數(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ù)在子類中可以重新構(gòu)造
virtual function:
class A{ virtual x() const=0;//純虛
virtual y();//虛}