C++面向對象高級編程 part5
2017-11-14 11:59:35 / herohlc
item1. 對象模型:vptr與vtbl
1. 類對象內存模型
class A{
public:
virtual void vfunc1();
virtual void vfunc2();
void func1();
void func2();
private:
int m_data1;
int m_data2;
};
class B :public A{
public:
virtual void vfunc1();
void func2();
private:
int m_data3;
};
class C :public B{
public:
virtual void vfunc1();
void func2();
private:
int m_data1;
int m_data4;
};
- 注意圖中的類對象模型,類對象中僅包含成員數據/vptr,不包括函數地址。
- deriverd類數據成員可以與base類數據成員重名,兩者保存在不同的區域。
- base類有虛函數,base類就會有vptr,base類對象有vptr則子類一定有vptr。
擴展:dervied class 調用base class 函數/函數覆蓋
#include <iostream>
using std::cout;
using std::endl;
class A{
public:
virtual void vfunc1(){};
virtual void vfunc2(){};
void func1(){ cout << "A::func1" << endl;};
void func2(){ cout << "A::func2" << endl;};
private:
int m_data1;
int m_data2;
};
class B :public A{
public:
virtual void vfunc1(){};
void func2() {
cout << "B::func2" << endl;
};
private:
int m_data3;
};
class C :public B{
public:
virtual void vfunc1(){};
void func2(){ cout << "C::func2" << endl;};
private:
int m_data1;
int m_data4;
};
int main() {
A a;
B b;
C c;
b.func1();
b.func2();
c.func1();
c.func2();
return 0;
}
A::func1
B::func2
A::func1
C::func2
- derived 類繼承了base類的函數調用權,所以可以通過derived對象調用base類的接口。
- derived 類的與base類的同名接口,derived會覆蓋base類。
注意??:C++繼承都繼承了哪些東西
- 數據
- 函數調用權。不是繼承函數相關的內存
2. vptr vs. vtbl
base類有虛函數,base類就會有vptr,base類對象有vptr則derived類一定有vptr。
虛函數的繼承方式
- dervied 類如果不override base類的虛函數,則直接繼承base類的虛函數
- derived 類如果override base類的虛函數,則在虛表中替換base類的虛函數地址
- 多重繼承中,虛函數不會“跳著”間接繼承,而是繼承自己的base類的虛函數。上圖中c直接繼承b的虛函數,而不是直接繼承自a的虛函數。
繼承關系下生成的函數
關注上圖中的a/b/c 三個類中生成的所有的函數,分成虛函數,非虛函數兩種類型。
vtbl
3. 靜態綁定 vs. 動態綁定
靜態綁定的函數調用方式
函數調用時,執行call xxx(地址)。
動態綁定的函數調用方式
通過指針找到對象的vtbl,然后找到正確的函數地址。
解釋成代碼形式如下:
(*(p->vptr)[n])(p)
(*(p->vptr)[n])(p)
中的n與虛函數的聲明順序一致
4. 多態的示例
多態解決的問題
1. 如何用一個只能容納一種元素類型的容器,存儲不同類型的元素?
容器保存的元素類型為,具有繼承關系的base類指針,其指向對象為dervied類對象。
2. 如何讓容器中的元素(base類指針),調用相同的接口卻具有不同的行為?
接口為虛函數。
多態的條件= up-cast pointer+ virtual function
item2. 對象模型:this
1. this指針參與到多態中
- 利用多態機制,在base類的成員函數流程中實現通用的邏輯,base類提供通用的接口,通用接口的實現可以遲后由derived 類實現。
- 成員函數通常是通過對象調用(static成員函數除外),所以在調用函數時編譯器會知道哪個對象在調用函數,此時當前對象的指針會傳遞給該成員this指針。
注意??:
- 編譯器在執行虛函數的調用(通過指針的方式)時,是通過指針指向內存單元的vptr找到vtbl中的函數地址,最后去調用地址中的函數。從實際的底層實現機制更容易解釋多態語法。
item3. 對象模型:dynamic binding
1. 從匯編的角度解釋dynamic binding
- 上圖中
a.vfunc1()
的調用為static binding。 - static binding的匯編執行形式:
call xxxx
- dynamic binding 匯編執行等價于 c的形式
(*(p->vptr)[n])(p))
注意??
用對象(非指針)的方式調用成員函數,不會造成多態。
item4. const
1. const member function
注意??
const member function中const修飾成員函數的形式只能用在成員函數中,不能用在全局的函數中。理解:這里的const是用來修飾this指針的,全局函數沒有this指針。
2. const obj vs. non-const obj vs. const member function vs. no-const member function
const member function中的const的作用,承諾該成員函數不會修改對象內容。本質是將this指針聲明為const*const形式。
xx | const object | non-const object |
---|---|---|
const member function | v | v |
non-const member function | x | v |
在設計類接口時要考慮const
class String{
public:
print(){} // bad , non-const print
}
const String str("hello");
str.print(); // error,
建議:如果member function 不改變對象數據,應該將該member function 聲明為const。否則const object 無法調用該接口。
在設計類的接口時就要確定要不要加const。
引申:函數(全局/class member function)的形參,如果不想改變實參,要將其聲明為const &
成員函數的const 和non-const 版本共存時,調用誰?
class string{
charT operator[](size_type pos)const
{/*不考慮copy on write*/}
reference operator[](size_type pos)
{/* 必須考慮copy on write*/}
}
}
string a;
cout << a[1]; // 調用 non-const operator[]
// non-const operator[] ?
a[1] = 'a'; // 調用 non-const operator[]
// non-const operator[] ?
- const 作為函數簽名的成分
- reference 返回值類型的函數可以作為左值。 因此上面代碼中non-const版本中的operator[] 可以作為左值使用,需要考慮copy on write,但const 版本的operator[] 返回值類型為charT 智能是右值不必考慮copy on write。
- 函數設計要考慮是否會把函數作為左值使用。
注意??
當成員函數的const和non-const版本同時存在,const object只會調用const版本,non-const object 只會調用non-const版本。
item5. new & delete
1. new /delete 表達式 vs. operator new /delete
- new /delete表達式 實現中會分解為多個步驟,其中包括調用operator new/delete。
注意??:
- new /delete 表達式 不能被重載,operator new /delete 可以被重載。
- operator new /delete 是對內存的操作.operator delete不包含調用析構函數的動作,析構函數在delete 表達式中調用。
item6. 重載operatro new /delete
1. 重載全局operator new/delete
inline void* operator new(size_t size){
cout << "my new :" <<size<< endl;
return malloc(size);
}
inline void* operator new[](size_t size) {
cout << "my new [] : " << size<< endl;
return malloc(size);
}
inline void operator delete(void* ptr){
cout << "my delete" << endl;
free(ptr);
}
inline void operator delete[](void * ptr) {
cout << "my delete[]" << endl;
free(ptr);
}
- operator new 需要一個size參數
- operator new 不是由用戶調用,由編譯器在expression new中調用
- 注意返回值類型為void*
- operator new 只需指定 size
- operator delete需要指定地址和可選的size
2. 重載member operator new/delete
class Foo {
public:
void*operator new(size_t size) {
cout << "Foo new" << endl;
return malloc(size);
}
void operator delete(void* ptr){
cout << "Foo delete" << endl;
free(ptr);
}
};
int main() {
Foo* f = new Foo;
delete f;
return 0;
}
Foo new
Foo delete
類如果重載operator new/delete ,在調用expression new 創建類對象時,expression new中將調用類的operator new。
expression new /delete分解過程
3. 重載member operator new[]/delete []
class Foo {
public:
void*operator new[](size_t size) {
cout << "Foo new [] : " << size <<endl;
return malloc(size);
}
void operator delete[](void* ptr){
cout << "Foo delete[] : " << ptr << endl;
free(ptr);
}
};
expression new[] /delete[]分解過程
- 注意構造和析構的調用次數,operator new中傳入的size值。
item7. 示例
class Foo {
public:
void* operator new(size_t size) {
cout << "Foo new" << endl;
return malloc(size);
}
void operator delete(void* ptr){
cout << "Foo delete" << endl;
free(ptr);
}
void*operator new[](size_t size) {
cout << "Foo new [] : " << size <<endl;
return malloc(size);
}
void operator delete[](void* ptr, size_t size){
cout << "Foo delete[] : " << ptr << endl;
free(ptr);
}
private:
int _id;
long _data;
string _str;
};
1. 如何使用全局的operator new / delete
Foo * p = ::new Foo;
::delete p
2. operator new 傳入的size 大小
operator new[] 需要分配一個保存對象個數的內存單元。
item8. 重載 new(), delete()
class member operator new() - placement new
1. class member placement new
示例
Foo* pf = new(300,'c')Foo
- class member 可以重載 operator new,寫出多個版本的operator new()
- 每個版本的聲明必須具有獨特的參數列,其中第一個參數必須是size_t,其余參數以new 所指定的placment arguments 為初值。
placement argument:new (…)小括號中的參數。size_t在聲明時默認需要定義,調用處不用顯示指定,size_t 不是 使用時(…)中的參數。
2. class member operator delete () - placement delete
絕不會被 expression delete 調用,只有當調用 placement new 之后調用的ctor拋出異常才會被調。
- class member operator delete () 不是必須定義的,如果定義要與placement new對應。
3. 示例
class Foo {
public:
Foo(){};
Foo(int a){
throw a;
};
void* operator new(size_t size, int extra){
return malloc(size+extra);
}
void operator delete(void* ptr, int extra){
cout << "placement delete called" << endl;
}
private:
int _id;
long _data;
string _str;
};
int main() {
Foo* a = new(1)Foo;
Foo* b = new(1)Foo(1);
return 0;
}
item9. basic_string使用new(extra)擴充申請量
1. why using placement new
如果想在new對象時創建額外超過對象大小的內存,使用placement new 代替 默認的new。