notes of 《Inside the C++ object model》
来源:互联网 发布:淘宝店标网站 编辑:程序博客网 时间:2024/05/20 12:25
A default constructor synthesiszes only when the implementation needs it.When the implementation needs it?
1.1,Member class object with default constructor.
1.2,Base class with default constructor.
1.3,Class with a virtual function.
1.4.Class with a virtual base class.
within the synthesized default constructor, only the base class subobjects and member class objects and
initialized. All other nonstatic data members, such as integers, pointers to integers, arrays of integers, and so on, are not initialized.
Programmers new to C++ often have two common misunderstandings:
1, That a default constructor is synthesized for every class that does not define one
2. That the compiler-synthesized default constructor provides explicit default initializers for each data member declared within the class
In practice, a good compiler can generate bitwise copies for most class objects since they have bitwise copy semantics.
When are bitwise copy semantics not exhibited by a class? There are four instances:
2.1. When the class contains a member object of class for which a copy constructor exists(either explicitly
declared by the class designer, as in the case of the previous String class, or synthesized by the compiler, as in the case of class Word)
2.2. When the class is derived form a base class for which a copy constructor exists(again, either explicitly declared or synthesized)
2.3. When the class declares one or more virtual functions
2.4. When the class is derived form an inheritance chain in which one or more base classes are virtual
3,program transformation semantics
Application of the copy constructor requires the compiler to more or less transform portion of your program.
In particular, consider a function that returns a class object by value for a class in which a copy constructor is
either explicit defined or synthesized. The result is profound program transformations both in the definition anduse of the function. Also, the compiler optimizes away the copy constructor invocation where possible, replacing
the NRV with an additional first argument within which the value is stored directly. Programmers who understand
these transformation and the likely conditions for copy constructor optimization can better control the runtime
performance of their programs.
4, member initialization list
the compiler iterates over and possibly reorders the initialization list to reflect the declaration order of the
members. It inserts the code within the constructor body prior to any explicit user code.
4.1 static function characteristics:
4.1.1, It cannot directly access the nonstatic members of its class.
4.1.2, It cannot be declarec const, volatile, or virtual.
4.1.3, It does not need to be invoked througn an object of its class, although for convenience,it may.
4.2 Pointer-to-Member Functions
4.2.1 Use of a pointer to member would be no more expersive than a nonmember pointer to function if it weren't for
virtual funcations and multiple inheritance(including,of course, virtual base class),which complicate both the type
and invocation of a pointer to member.
5 Semantics of Construction, Desctruction, and Copy
In general,the data members of a class should be initialized and assigned to only within the constructor add other
member functions of that class.To do otherwise breaks encapsulation, thereby making maintenance and modification of
the class more difficult.
5.1 A better design is to not declare a virtual destructor as pure;
5.2 It is a bad design choice to decalre all functions virtual and to depend on the compliler to optimize away
unnecessary virtual invocations.
5.3 Considerable more problematic is declaring a virtual fucntion const and then discovering that,in pratice,a
derived instance really does need to modify a data member.In my code, I tend toward leaving off the const;
5.4 In C,global is treated as a tentative definition because it is without explicit initialization.A tentativedefinition can occur multiple times within the program.While global is treated within C++ as a full definition.All
global objects within C++ are treated as initialized.
code or the expansion of members initialized within the member initialization list.
recommendation:Do not declare data within any class that serves as a virtual base class.
as Plain Ol'Data, then as an abstract data type(ADT), and then as single, multiple, and virtual inheritances are incorporated in turn.
6.1 In general,place an object as close as possible to the code segment actually using it.Doing this can save you
unnecessary object creation and destruction.
- notes of 《Inside the C++ object model》
- Inside The C++ Object Model ---- The Semantics of Constructors
- Inside The C++ Object Model ---- The Semantics of Data
- 《Inside C++ Object Model》 Chapter 2 : The semantics of Constructor
- Inside in C++ Object Model Notes(1)
- Inside The C++ Object Model
- Inside the C++ Object Model
- 《Inside the C++ Object Model》
- Inside The C++ Object Model
- Inside The C++ Object Model
- Inside The C++ Object Model
- inside the c++ object model
- Inside The C++ Object Model ---- Object Lessons
- inside the C++ object model chapter 4 the semantics of function学习笔记
- inside the C++ object model chapter 3 the semantics of data
- 读Inside the C++ Object Model有感
- Inside The C++ Object Model 笔记
- 《Inside the C++ Object Model》拿到了
- spring与ibatis的整合及多数据库连接
- MySQL server has gone away错误提示解决方法
- 用JS & HTML5渲染3D花朵(程序艺术)
- 情人节记
- 2012-2013年度总结——思想篇
- notes of 《Inside the C++ object model》
- 从细节处窥探Vb
- 天勤OJ 题目1386: 围圈报数
- zoj 1813 Biker's Trip Odometer
- MVC3学习~
- 时间管理的感受篇
- 瑞雪兆丰年——与Vb的第一次相遇相知
- 你若安好,便是晴天
- css常用综合案例