`
anzhsoft
  • 浏览: 23330 次
  • 性别: Icon_minigender_1
  • 来自: 天津
文章分类
社区版块
存档分类
最新评论

C++对象模型(一):The Semantics of Constructors The Default Constructor (默认构造函数什么时候会被创建出来)

 
阅读更多

本文是 Inside The C++ Object Model, Chapter 2的部分读书笔记。

C++ Annotated Reference Manual中明确告诉我们: default constructor会在需要的时候被编译器产生出来。注意,这里是编译器需要,而不是程序需要。后来的C++ Standard 95修改了这种说法,但是实质上仍是相同的: For class X, if there is none user declared constrator, one default constructor will be implicitly generated by the Compiler.

但实际上,如果default constructor是trivial(无用的),那么编译器根本就不会产生它!只有一下四种情况,non-trivial default constructor会被产生出来,来保证C++语言的机制能够按照预期工作:

1)带有Default Constructor的Member Class Object

如果该class X 包含有带有默认构造函数的成员class object (member object),那么编译器需要对此class X合成一个default constructor。不过该合成只在该合成操作真正被调用时。

被合成的这个default constructor,包含有调用这个member class的default constructor的代码,但是它不会初始化它自身其他的成员变量,比如char *str; int data;这些成员变量的初始化时设计者的责任,而不是编译器的职责!

由此有进一步的思考,如果是设计者已经定义了constructor,来初始化比如char *str; int data; 那么编译器如何初始化其他的member class object呢? 答案就是编译器需要扩张该class的constructor,以按照member class object的声明顺序来调用各个class的default constructor。


2) 带有Default Constructor的Base Class

如果class X继承自一个带有default constructor的class, 那么编译器将为这个class生成non-trivial的default constructor,并且按照base class的顺序逐次调用。

如果user declared 许多constructors,但是没有default constructor(就是没有任何参数的那个constructor),那么编译器不会产生default constructor了。而是扩张每个constructor,使其包含必要的default constructor的扩张代码,比如初始化它的member class object。 参考第一种情况


3) 带有virtual function

对于有virtual function的class,一个virtual function table会被编译器产生出来,存着virtual functions的地址。 而在每一个class的object中,会有一个pointer member(称为vptr)会被编译器生成出来,内容那个class virtual function table的地址。

所以编译器为了使得virtual function的机制生效,必须要为每个这种class的object生成合理的vptr,而vptr的赋值就发上在扩张后的constructors里。如果class没有任何的constructors,那么default constructor会被认为是non-trivial并且会被生成。如果class有constructor/s,那么所有的constructor都会扩张以赋予vptr以合理的值。


4)带有一个virtual base class的class

Virtual base class的实现在不同 的编译器间有极大的差异。共同点就是virtual base class 在其每一个derived class object的位置,在执行期间能够准备妥当。这些工作都要放到constructor中去完成。


总结
在合成的default constructor中,只有base class subobjects 和member class objects才会初始化。所有其他的nonstatic data member,都不会被初始化,这些工作应该由设计者(程序猿)而不是编译器去完成。

分享到:
评论

相关推荐

    深度探索C++对象模型

    深度探索C++对象模型 第0章 导读(译者的话) 第1章 关于对象(Object Lessons) 加上封装后的布局成本(Layout Costs ...第2章 构造函数语意学(The Semantics of constructors) 2.1 Default Constructor的建构操作

    深度探索模C++对象模型PDF

    第2章 构造函数语意学(The Semantics of constructors) 2.1 Default Constructor的建构操作 “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual ...

    深度探索C++对象模型 超清版

    第2章 构造函数语意学(The Semantics of constructors) 2.1 Default Constructor的建构操作 “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual ...

    《深度探索C++对象模型》(Stanley B·Lippman[美] 著,侯捷 译)

    第2章 构造函数语意学(The Semantics of constructors) 2.1 Default Constructor的建构操作 “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual ...

    Inside The C++ Object Model 英文版

    oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"--virtual functions and virtual inheritance. This book shows how...

    C++对象模型

    oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how ...

    A tour of C++ 2nd

    In A Tour of C++, Second Edition, Bjarne Stroustrup, the creator of C++, describes what constitutes modern C++. This concise, self-contained guide covers most major language features and the major ...

    Inside the C++ object model 高清英文版

    oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how ...

    the formal semantics of programming language: an introduction

    本书为英文原版,内容包括程序语言设计的基本原理,以及关于语言设计的形式化语义的描述理论基础,内容较为专业,却是一本非常优秀的关于形式化语义的教材!

    Inside the C++ Object Model

    oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how ...

    The C++ Standard Library 2nd 原版pdf by Josuttis

    I never thought that the first edition of this book would sell so long. But now, after twelve years, it’s time for a new edition that covers C++11, the new C++ standard. Note that this means more ...

    A Tour of C++, 2nd Edition

    A Tour of C++ (2nd Edition) (C++ In-Depth Series) By 作者: Bjarne Stroustrup ISBN-10 书号: 0134997832 ISBN-13 书号: 9780134997834 Edition 版本: 2 出版日期: 2018-07-09 pages 页数: 815 In A Tour of ...

    The formal semantics of programing language

    In giving a formal semantics to a programming language we are concerned with building a mathematical model. Its purpose is to serve as a basis for understanding and reasoning about how programs behave...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Classes Doing Work in Constructors Default Constructors Explicit Constructors Copy Constructors Structs vs. Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control ...

    The Proof Theory and Semantics of Intuitionistic Modal Logic

    Possible world semantics underlies many of the applications of modal logic in computer science and philosophy. The standard theory arises from interpreting the semantic de nitions in the ordinary meta...

    Inside the C++ Object Model_english&chinese;

    programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how your ...

    Inside the C++ object model

    oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"--virtual functions and virtual inheritance. This book shows how...

    Making C++ Objects Persistent: the Hidden Pointers

    Our solutions are elegant in that they do not require modifying the C11 compiler or the semantics of C11. We also discuss another problem that arises because C11 allows base class pointers to point to...

    Semantics of Asynchronous JavaScript

    Since the JavaScript language is single threaded, Node.js programs must make use of asynchronous callbacks and event loops managed by the runtime to ensure appli- cations remain responsive....

Global site tag (gtag.js) - Google Analytics