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

C++对象模型(二):The Semantics of Copy Constructors(拷贝构造函数之编译背后的行为)

 
阅读更多

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

有三种情况,需要拷贝构造函数:

1)object直接为另外一个object的初始值

2)object作为函数以值传递的参数

3) object以函数返回值形式返回


如果class没有提供一个explicit copy constructor时,编译器会以default memberwise initialization,也就是把每一个内建的或者派生的data member的值,从某个object直接拷贝到另外一个object上,对于member class object,以递归的方式施行memberwise initialization。

编译器并不会生成trivial的copy constructor,除非是bitwise copy semantics,以下四种情况就是非bitwise copy semantics:

1)class的某个member object声明有一个copy constructor时,这个copy constructor不会是被设计者声明的,或者是被编译器合成的

2)class继承自的base class含有copy constructor时

3)class含有virtual functions

4)class的base class是virtual时

编译器需要合成copy constructor,并且调用member object或者base class的copy constructor。


对于virtual function或者virtual inheritance,编译器需要为每一个object设计好vptr以便在runtime获取到正确的virtual function table。因为vptr处理不好有可能发生对象的sliced,导致出现blow up。


总结:

如果class是bitwise copy semantics,那么编译器不会合成copy constructor,只是按照default memberwise initialization copy source object到destination object。否则,编译器将合成copy constructor,以调用member object或者base class的copy constructor或者使得vptr设置好,使得在runtime可以使virtual base class或者base class可以正确访问virtual functions。


注意,本文只是说明编译器在copy constructor上为我们做了什么,并不是编译器合成的copy constructor就是安全可用的。这里需要和deep copy(深拷贝)/shallow copy(浅拷贝)加以区别。和编译器是否为我们合成default constructor一样,它只是完成自己的职责,并不会为non class member object and non-static members 初始化。 如果需要deep copy,设计者必须explicit declare copy constructor。否则即使编译器合成了,也会使得程序出现undefined behaviors。

分享到:
评论

相关推荐

    深度探索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 ...

    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 ...

    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 ...

    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...

    A Tour of C++, 2nd Edition

    Stroustrup presents the C++ features in the context of the programming styles they support, such as object-oriented and generic programming. His tour is remarkably comprehensive. Coverage begins with ...

    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...

    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 ...

    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....

    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 ...

    Formal Syntax And Semantics of Programming Languages

    With this book, readers with a basic grounding in discreet mathematics will be able to understand the practical applications of these difficult concepts. The book presents the typically difficult ...

    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...

    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 ...

Global site tag (gtag.js) - Google Analytics