C++空類詳解
空類默認(rèn)產(chǎn)生的成員:
class Empty {};
Empty(); // 默認(rèn)構(gòu)造函數(shù)
Empty( const Empty& ); // 默認(rèn)拷貝構(gòu)造函數(shù)
~Empty(); // 默認(rèn)析構(gòu)函數(shù)
Empty& operator=( const Empty& ); // 默認(rèn)賦值運算符
Empty* operator&(); // 取址運算符
const Empty* operator&() const; // 取址運算符 const
給出一個例子:
#include<iostream>
using namespace std;
class Empty
{
public:
Empty *operator&()
{
cout<<"AAAA"<<endl;
return this;
}
const Empty* operator&() const
{
cout<<"BBBB"<<endl;
return this;
}
};
int main(void)
{
Empty e;
Empty *p=&e;
const Empty e2;
const Empty *p2=&e2;
cout<<sizeof(Empty)<<endl;
}
運行結(jié)果:

可見:
Empty *p=&e調(diào)用了Empty* operator&()運算符函數(shù)
const Empty *p2=&e2調(diào)用了const Empty* operator&() const運算符函數(shù)。
空類的大小為1字節(jié)。
相關(guān)文章
離線安裝visual?studio2022+QT5.12的實現(xiàn)步驟
近期有需求離線配置C++與QT環(huán)境,本文主要介紹了離線安裝visualstudio2022+QT5.12的實現(xiàn)步驟,具有一定的參考價值,感興趣的可以了解一下2024-06-06
C/C++ Crypto密碼庫調(diào)用的實現(xiàn)方法
Crypto 庫是C/C++的加密算法庫,這個加密庫很流行,基本上涵蓋了市面上的各類加密解密算法,感興趣的可以參考一下2021-06-06
C語言結(jié)構(gòu)體成員賦值的深拷貝與淺拷貝詳解
C語言中的淺拷貝是指在拷貝過程中,對于指針型成員變量只拷貝指針本身,而不拷貝指針?biāo)赶虻哪繕?biāo),它按字節(jié)復(fù)制的。深拷貝除了拷貝其成員本身的值之外,還拷貝成員指向的動態(tài)內(nèi)存區(qū)域內(nèi)容。本文將通過示例和大家詳細(xì)說說C語言的深拷貝與淺拷貝,希望對你有所幫助2022-09-09
C/C++自主分配出現(xiàn)double free or corruption問題解決
這篇文章主要為大家介紹了C/C++出現(xiàn)double free or corruption問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
OpenCV cv.Mat與.txt文件數(shù)據(jù)的讀寫操作
這篇文章主要介紹了OpenCV cv.Mat 與 .txt 文件數(shù)據(jù)的讀寫操作,現(xiàn)在分享給大家,也給大家做個參考2018-05-05

