Chap2-構造函數語意學

如果一個類沒有任何constructor,那么會有一個default constructor被隱式的聲明出來,一個implicit default constructor將是一個trivial(無用的)constructor。但是在某些情況下,implicit default constructor將是一個nontrivial constructor,下面一一討論:

由編譯器合成nontrivial default constructor的四種情況:

1)帶有default constructor 的 member class object(ps:不包括基本數據類型對象):

  • case 1:如果一個類沒有任何constructors,但它包含一個member class object,那么這個類的implicit default constructor 會去調用member class object的default constructor。

class A
{
public:A(){cout<<"Class A Constructor!"<<endl;}
};class B
{
public:A a;   // a is a member class object and class A has a default constructor.
};
void main()
{B b;   
}


  • case 2:如果一個類包含constructors(ps:不限定是default constructor),也包含帶default constructor的member class object,但是沒有在constructors中顯示調用,那么編譯器會擴張已存在的constructors,使其每個member class object得到初始化。
class A
{
public:A(){cout<<"Class A Constructor!"<<endl;}
};
class B
{
public:B(int){}  // Non-default constructorA a;   // a is a member class object and class A has a default constructor.
};
void main()
{B b(5);   
}


  • case 3:C++中以member class object 在類中的聲明順序來調用各個constructors。
class A
{
public:A(){cout<<"Class A Constructor!"<<endl;} // Default constructor
};
class B
{
public:B(){cout<<"Class B Constructor!"<<endl;}  // Default constructor
};
class C
{
public:A a;  // Member class objectB b;  // Member class object
};
void main()
{C c;   
}


2)帶有default constructor 的 base class:
  • case 1:如果一個類沒有任何constructors,但它派生自一個或多個帶有default constructor 的 base class,那么這個類的implicit default constructor 會去調用base class 的 default constructor(按聲明順序調用)。
class A
{
public:A(){cout<<"Class A Constructor!"<<endl;} // Default constructor
};
class B : public A{};
void main()
{   B b;
}


  • case 2:如果一個類包含constructors(ps:不限定是default constructor),且派生自一個或多個帶有default constructor 的 base class,但是沒有在constructors中顯示調用,那么編譯器會擴張已存在的constructors,使其每個上層base class 的 default constructor 得到調用(按聲明順序調用)。
class A
{
public:A(){cout<<"Class A Constructor!"<<endl;} // Default constructor
};
class B : public A
{
public:B(){}
};
void main()
{  B b;
}


3)帶有一個或多個virtual functions 的 class:
  • class聲明(或繼承)一個或多個virtual functions,那么編譯器會在constructors(如果沒有,則隱式創建一個default constructor)中隱式的進行一些擴張行動:創建一個virtual function table,內放class 的 virtual functions的地址(所以一個類中如果聲明了virtual function(純虛函數除外),就必須實現它,實現了才會有函數地址);在每一個class object中,創建一個額外的vft_ptr,內含virtual function table的地址。
// sizeof(A)=1
class A
{
public:A(){cout<<"Class A Constructor!"<<endl;} // Default constructor
};// sizeof(A)=4
class A
{
public:A(){cout<<"Class A Constructor!"<<endl;} // Default constructorvirtual void foo(){}
};

4)帶有一個或多個virtual base class 的 class:
  • class或繼承一個或多個virtual base class,那么編譯器會在constructors(如果沒有,則隱式創建一個default constructor)中隱式的進行一些擴張行動:創建一個virtual base class table,內放virtual base class subobjects 的地址(所以要virtual派生自一個base class,那么這個base class必須提供default constructor,使得編譯器能創建virtual base class 的 subobject);在每一個class object中,創建一個額外的vbt_ptr,內含virtual base class table的地址。
// sizeof(B)=1
class A{};
class B : public A{};
// sizeof(B)=4
class A{};
class B : public virtual A{};

總結:以上四種情況,會造成編譯器為未聲明constructor的類隱式生成一個default constructor,或者擴張已有的constructors使其滿足編譯器的需要。

轉載于:https://www.cnblogs.com/newhand-liu/archive/2012/05/26/2793274.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274787.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274787.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274787.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【熱點】React18正式版發布,未來發展趨勢是?

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列2022年…

不要重新發明輪子_是否重新發明輪子

不要重新發明輪子Design is a profession that thrives on creativity. Us designers are constantly trying to innovate by thinking outside the box. We’ve seen design evolve across all sectors — print, digital, product, architecture etc. We have gone from type…

asp.net mvc批量刪除的實現

<form action"Index" method"post"> <div> {<table><thead> <tr> <th width"100">編號</th><th width"100">名字</th></tr></thead> <tbody> foreach (var…

點擊頁面元素,這個Vite插件竟然幫我打開了Vue組件文件!超級好用!

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列前言這…

shields 徽標_符號,標志,文字標記:徽標類型的綜合指南

shields 徽標Designers and non-designers alike struggle with common terminology when talking about brand marks, often using different terms interchangeably. When it comes to clarifying definitions, sometimes even the most seasoned professionals get confused…

【原創】SVM小結

理論基礎&#xff1a; 機器學習有三類基本的問題&#xff0c;即模式識別、函數逼近和概率密度估計&#xff0e; SVM有著嚴格的理論基礎&#xff0c;建立了一套較好的有限訓練樣本下機器學習的理論框架和通用方法。他與機器學習是密切相關的&#xff0c;很多理論甚至解決了機器學…

React 18 帶給我們的驚喜

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列這篇文…

建模心法(2)——邁出建模第一步

原文地址&#xff1a;http://www.cnblogs.com/1-2-3/archive/2008/08/04/model-method-part1.html 原文作者&#xff1a;景春雷 一錯再錯的這故事才精彩 ——樸樹 《我愛你再見》摘要 即使讀了再多的書、跟過再多的項目&#xff0c;…

Web:你知道我這十幾年是怎么過來的嗎?!

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列1989 …

設計師更高效_如何丟掉我的工作使我成為一名更好的設計師

設計師更高效I lost my job a few times early on in my design career. In the process of getting back up after a job loss, it has made me a better designer not only in terms of hard skills but the soft skills required to be more resilient and empathetic, whic…

【ASP.NET】登陸成功后如何跳轉到上一個頁面

當用戶瀏覽網頁的時候會在某個地方需要用戶登陸才能繼續瀏覽&#xff0c;用戶登陸之后會自動跳轉到剛剛瀏覽的頁面。這個步驟是怎么實現的呢&#xff1f;net小伙在查閱相關資料實踐之后終于明白了&#xff0c;其實很簡單&#xff0c;先分享給大家吧。 當用戶在瀏覽一個頁面的時…

4月,誠邀你參加源碼共讀,學會看源碼,打開新世界!開闊視野

大家好&#xff0c;我是若川。很多關注我的新朋友可能不知道我組織了源碼共讀活動~也有很多人不知道我是誰。有人以為我是80后。有人以為我是全職自媒體等等。若川的 2021 年度總結&#xff0c;彈指之間 這篇文章寫了我是16年畢業的&#xff0c;或許有些啟發。源碼共讀按照從易…

bt709和srgb_選擇用于多用途視頻編輯和色彩校正的顯示器— sRGB,DCI-P3,REC 709

bt709和srgb**Note from the author: if you enjoy this article, please follow me or this publication for more video production and marketing related content.****作者注&#xff1a;如果您喜歡本文&#xff0c;請關注我或此出版物以獲取更多與視頻制作和營銷相關的內容…

超4000人參加源碼共讀,喊你來一起學習成長~打開新世界

大家好&#xff0c;我是若川。很多關注我的新朋友可能不知道我組織了源碼共讀活動~也有很多人不知道我是誰。有人以為我是80后。有人以為我是全職自媒體等等。若川的 2021 年度總結&#xff0c;彈指之間 這篇文章寫了我是16年畢業的&#xff0c;或許有些啟發。源碼共讀按照從易…

figma設計_如何在Figma中構建設計入門套件(第二部分)

figma設計Figma教程 (Figma Tutorial) With this short, but informative Tutorial Series I aim to show you how to build the solid foundations of a powerful, and versatile Design Starter Kit, enabling you to start your next project in Figma faster than ever bef…

Hibernate 簡介(百度)

Hibernate是一個開放源代碼的對象關系映射框架&#xff0c;它對JDBC進行了非常輕量級的對象封裝&#xff0c;使得Java程序員可以隨心所欲的使用對象編程思維來操縱數據庫。 Hibernate可以應用在任何使用JDBC的場合&#xff0c;既可以在Java的客戶端程序使用&#xff0c;也可以在…

GitHub 最受歡迎的Top 20 JavaScript 項目

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列今天來…

java反編譯,eclipse支持插件

http://java.decompiler.free.fr/?qjdeclipse 按照說明 在eclipse更新插件就可以。 這樣 在一些 閉源的jar文件&#xff0c;你也可以看到 大致的源碼。&#xff08;公司 知道如何 加密混淆 java代碼或class文件&#xff0c;居然無法使用jd-gui瀏覽源碼&#xff09; 而&#xf…

unity vr 交互_基于手動的VR / MR交互,用于刪除實體

unity vr 交互Deleting an entity or closing an application is one of the most ubiquitous operations performed in any application. It is necessary for the organization of the data. On the computer, there are multiple ways to delete a file like cmd delete, d…

手把手帶你走進Babel的編譯世界

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列前言談…