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)賦值運(yùn)算符
Empty* operator&(); // 取址運(yùn)算符
const Empty* operator&() const; // 取址運(yùn)算符 const
給出一個(gè)例子:
#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;
}
運(yùn)行結(jié)果:

可見:
Empty *p=&e調(diào)用了Empty* operator&()運(yùn)算符函數(shù)
const Empty *p2=&e2調(diào)用了const Empty* operator&() const運(yùn)算符函數(shù)。
空類的大小為1字節(jié)。
相關(guān)文章
深入學(xué)習(xí)C語言mmap和shm*的使用方法技巧
本文將詳細(xì)介紹mmap和shm的工作原理,包括它們在內(nèi)存映射和共享內(nèi)存方面的優(yōu)勢和適用場景,同時(shí),文章還會(huì)分享一些使用mmap和shm的技巧和經(jīng)驗(yàn),以幫助讀者優(yōu)化并提高程序性能,使你能夠在實(shí)際項(xiàng)目中更好地利用這些技術(shù)來加速數(shù)據(jù)共享和多線程應(yīng)用2023-10-10C++實(shí)現(xiàn)第K順序統(tǒng)計(jì)量的求解方法
這篇文章主要介紹了C++實(shí)現(xiàn)第K順序統(tǒng)計(jì)量的求解方法,很有借鑒價(jià)值的算法,需要的朋友可以參考下2014-08-08C++實(shí)現(xiàn)LeetCode(44.外卡匹配)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(44.外卡匹配),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++單例模式為何要實(shí)例化一個(gè)對(duì)象不全部使用static
這篇文章主要介紹了C++單例模式為何要實(shí)例化一個(gè)對(duì)象不全部使用static,文基于C++圍繞主題展開詳細(xì)內(nèi)容,需要的小伙伴可以參考一下2022-05-05