C++問題的補充
前言
關于對之前遺留的補充
- malloc 和 new 的區別
- const 和 參考 的深入
- this指標 的深入
一、C++中物件的創建
malloc和new創建物件
//定義一個Pointer類
class Pointer
{
public:
int row;
int col;
Pointer()
{
row = 0;
col = 0;
}
Pointer(int r, int c)
{
row = r;
col = c;
}
~Pointer()
{
cout << "Pointer" << endl;
}
};
malloc不能創建物件,需要配合定位new使用
//什么是 定位new
定位new運算式是在已分配的原始記憶體空間中呼叫建構式初始化一個物件,
換句話說就是,現在空間已經有了,不需要定位new像常規new一樣去給申請
空間,只需要定位new在已有的空間上呼叫建構式構造物件而已,
簡而言之,已存在空間,再構造物件,
malloc作用:
- 1、堆區申請空間
- 2、堆區無物件
//定位new,在已有(malloc申請的)空間上,構造物件
int main()
{
Pointer* s = (Pointer*)malloc(sizeof(Pointer));
new(s) Pointer(12, 23);
s->~Pointer();
free(s);
s = NULL;
return 0;
}
new創建物件的的程序:
- 1、呼叫malloc,從堆區申請空間
- 2、構建物件,呼叫建構式
- 3、物件地址空間回傳給p
//普通new,先申請空間,再創建物件
int main()
{
Pointer p1(100,200);
Pointer p;
p = new Pointer(10,20);
delete p;
return 0;
}
二、new 和 malloc 的區別
| 區別 | new | malloc |
|---|---|---|
| 屬性 | 關鍵字 | 庫函式 |
| 引數 | 無需指定記憶體塊大小,編譯器實作 | 需要指定大小 |
| 回傳型別 | 物件型別的指標 | void * |
| 多載 | 支持 | 不支持 |
| 記憶體 | 在free store上分配記憶體 | 在堆區空間分配記憶體 |
| 分配失敗 | bac_alloc例外 | 回傳NULL |
| 自定義型別 | 可以呼叫析構與構造 | 不可呼叫析構和構造 |
| 效率 | 高 | 低 |
三、C與C++的一個重要區別
C語言:有空間一定能操作
C++: 有空間不一定有物件,有物件一定要有空間
C
int main()
{
char str[10];
int* ip = (int*)str;
*ip = 100;
}
C++:有物件一定要有空間
//有物件一定有空間,即使是空類
class Test
{
};
int main()
{
Test t;
cout << sizeof(t) << endl; //空類大小為1
return 0;
}
空類的大小:
宣告型別時也需要占用一些空間
如果沒有這個空間,也就不知道該型別的存在,
空類具體占用的空間大小由編譯器決定,vs2022中大小為1
編譯器自動為其安插1個char,
//有空間可能沒物件
int main()
{
CGoods* p = (CGoods*)malloc(sizeof(CGoods));
cout << p->GetPrice() << endl; //結果:-4.31602e+08
}
四、this指標
1. 什么是this指標
例子代碼如下:
//一個商品型別
class CGoods
{
private:
char Name[21];
int Amount;
float Price;
float Total_value;
public:
void RegisterGoods(const char[], int, float); //注冊商品
void CountTotal(); //總共數量
void GetName(const char[]); //獲得名字
int GetAmount(void); //讀取商品
float GetPrice(void); //得到價格
float GetTotal_value(void); //總價
};
void CGoods::RegisterGoods(const char name[], int amount, float price)
{
strcpy_s(Name, 21, name);
//strcpy_s(this->Name, 21, name);
Amount = amount;
//this->Amount = amount;
Price = price;
//this->Price = price;
}
void CGoods::GetName(const char name[])
{
strcpy_s(Name, 21, name);
}
int CGoods::GetAmount(void)
{
return Amount;
}
float CGoods::GetPrice(void)
{
return (Price);
}
int main()
{
CGoods c1, c2, c3;
c1.RegisterGoods("C++", 12, 98.99);
c2.RegisterGoods("C++", 23, 15.9);
c3.RegisterGoods("C++", 44, 54.3);
return 0;
}
這里并沒有this的存在,那么this指標在哪呢?
首先來看一下C++模型物件的安排
方法一:各物件完全獨立的安排記憶體

上圖是系統為c1,c2,c3物件分配了全套的記憶體,包括安放成員資料的資料區和安放成員函式的代碼區,但是區別同一個類所實體化的物件,是由屬性(資料成員)的值決定,不同物件的資料成員的內容是不一樣的,而行為(操作)是用函式來描述的,這些操作的代碼對所有的物件都是一樣的,
方法二:資料區私有,代碼區公有

在C++中,通過公共代碼區節省了大量的空間,
那么物件是如何區分哪個是自己的資料區呢?
this指標就是解決這個問題的,
2. this指標的底層
首先明確幾個概念:
- this指標不屬于物件的一部分,不會影響sizeof(物件)的結果,
- 只有在類的非靜態成員函式中才可以使用this指標,其它任何函式都不可以,
- this的作用域在類成員函式的內部,
當在類的非靜態成員函式中訪問類的非靜態成員的時候,編譯器會自動將物件本身的地址作為一個隱含引數傳遞給函式,也就是說,即使你沒有寫上this指標,編譯器在編譯的時候也是加上this的,它作為非靜態成員函式的隱含形參,對各成員的訪問均通過this進行,
仍然是上一個例子:
//注釋為本質
//成員方法的第一個引數添加一個物件型別的this指標,
//成員方法中出現的所有成員屬性由this指標指向,
void CGoods::RegisterGoods(const char name[], int amount, float price)
//void CGoods::RegisterGoods(CGoods *this,const, const char name[], int amount, float price)
{
strcpy_s(Name, 21, name);
//strcpy_s(this->Name, 21, name);
Amount = amount;
//this->Amount = amount;
Price = price;
//this->Price = price;
}
int main()
{
c1.RegisterGoods("C++", 12, 98.99);
//物件 + . 呼叫成員方法的本質
//邏輯上面向物件,實際上是面向程序
//RegisterGoods(&c1, "C++", 12, 98.99);
}
這樣就不難理解,當通過 物件 + . 呼叫成員方法時,會將物件的地址作為第一個引數傳遞給成員方法,這也是一個物件資料區的唯一標識,得到該地址以后,呼叫成員函式中如果遇到成員屬性,就給每個屬性前面加上this,
3.this指標 的型別
this指標的型別為 : 型別名 * const this,
但是在VS2022中發現并沒有這個const,不知道是什么情況
4. C++類成員函式默認呼叫方式 與 c語言呼叫方式的區別
C++默認呼叫(C++成員函式的this指標)
void CGoods::RegisterGoods(const char name[], int amount, float price)
{
strcpy_s(Name, 21, name);
//strcpy_s(this->Name, 21, name);
Amount = amount;
//this->Amount = amount;
Price = price;
//this->Price = price;
}
void CGoods::GetName(const char name[])
{
strcpy_s(Name, 21, name);
}
int CGoods::GetAmount(void)
{
return Amount;
}
float CGoods::GetPrice(void)
{
return (Price);
}
int main()
{
CGoods c1, c2, c3;
c1.RegisterGoods("C++", 12, 98.99);
//C++的呼叫約定 lea ecx , [c1]
c1.CountTotal();
return 0;
}
物件 . 呼叫成員方法,邏輯上是面向物件,物理上還是面向程序,會將物件地址傳遞給成員函式的this指標,物件的地址傳遞時候,直接進行 lea ecx, [c1],到達成員函式內部時,使用mov指令,將ecx的值傳遞給this指標,


c語言呼叫方式(thiscall呼叫)
在成員函式前面加上__cdecl ,說明是C的呼叫約定,我們可以將c1的地址傳遞給eax,通過push指令入堆疊eax,直接傳遞給形參,
//通過使用__cdecl改寫 thiscall
//void __cdecl CGoods::RegisterGoods(const char name[], int amount, float price);


5、this 與 常物件
常物件只能呼叫常方法,普通物件能呼叫普通方法和常方法,
例子:
//main函式中,哪些陳述句能編譯通過
class CGoods //設計型別,不是定義型別
{
private:
char Name[21];
int Amount;
float Price;
float Total_value;
public:
CGoods(char name[], int amount, float price, float total_value)
{
strcpy_s(Name, name);
Amount = amount;
Price = price;
Total_value = https://www.cnblogs.com/baobaobashi/p/total_value;
}
void RegisterGoods(const char[], int, float);
float GetPrice(void)
{
return Price;
}
float GetTotal_value(void) const
{
return Total_value;
}
};
int main()
{
CGoods c1 = {"C++",12,23.0,23.0 };
const CGoods c2 = { "java",12,23 };
c1.GetPrice(); //right:普通物件呼叫普通方法可以通過this指標修改成員函式的值
c2.GetPrice(); //error:常物件不能呼叫普通方法,常物件呼叫普通方法時,成員屬性有修改的風險,所以報錯,
c1.GetTotal_value(); //right:普通物件既可以呼叫普通方法又可以呼叫常方法
c2.GetTotal_value(); //right:常物件只能呼叫常方法
}
我們知道了this指標的型別是 物件名 *const this,那么物件加 const 也就是const 物件名 *const this型別,為了給this指標所指向的物件限定,也就是不允許修改物件中的成員屬性,那么任何對成員屬性進行修改的行為都是錯誤的,
四、const 回顧
int main()
{
const int a = 10;
int b = 20;
int* p1 = &a; //error:可以通過p1改變a
const int* p2 = &a; //right:p2所指向的值不能修改
int* const p3 = &a; //error:可以通過p3改變a,但不能改變p3的指向
const int* const p4 = &a; //right:能力收縮
return 0;
}
五、建構式和解構式
1.建構式和解構式的用法
class Pointer
{
public:
int row;
int col;
//建構式:
//1、函式名與類名相同
//2、無回傳值
//3、物件實體化時,編譯器自動呼叫
//4、可以多載
Pointer() //無參建構式,預設構造
{
row = 0;
col = 0;
}
Pointer(int r, int c) //建構式多載
{
row = r;
col = c;
}
~Pointer() //解構式
{
cout << "Pointer" << endl;
}
//解構式:
//1、~ + 類名
//2、解構式無函式回傳值型別
//3、一個類只能一個也只能有一個解構式
//4、物件注銷時,系統會自動呼叫
//5、如果沒有給出解構式,系統會給出默認解構式
};
int main()
{
Pointer c1 = { 12,23 };
Pointer c2; //right:呼叫預設建構式
//Pointer c2(); //error:沒有引數不需要帶括號
Pointer c3(10, 20); //right:呼叫帶引數建構式
//不能通過物件手動呼叫建構式
//c2.Pointer(10,20); //error
//有空間不一定有物件,得要物件呼叫建構式來構建物件,才有空間
//有物件一定有空間
return 0;
}
2.建構式和建構式的執行順序
留給后面
class Object
{
private:
int val;
public:
Object(int x)
{
val = x;
cout << "create :" << val << endl;
}
};
Object o1(1);
int main()
{
Object o2(2);
}
Object o3(3);
已解決:程式執行順序
總結
待解決的問題:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/541792.html
標籤:C++
