C++ Knowledge series 1
Programming language evolves always along with Compiler's evolvement.
1. The C++ Object Model:
Stroustrup's original (and still prevailing) C++ Object Model is derived from the simple object model by optimizing for space and access time. Non-static data members are allocated directly within each class object. Static data members are stored outside the individual class object. Static and non-static and virtual and non-virtual function members are also hoisted outside the class object (same as global function in C using name mangling, these functions are shared by all objects by passing ‘this’ parameter to function for manipulating the data encapsulated in object).
Virtual functions are supported in two steps:
- A table of pointers to virtual functions (vtbl) is generated for each class, not each object (this is called the virtual table, shared by all objects of class). That means every class or object has a vtbl if required. In these table, the address of virtual functions (also known as/a.k.a global functions) is stored in its slot.All methos of class wil be converted into a global method with the first parameter 'this' for mem-funcion using name mangling. The non-member function has not 'this' parameter.
- A single pointer to the associated virtual table is inserted or reset within each class object (traditionally, this has been called the vptr which pertains to object when creating or copying or assigning a object only for reference or pointer). The setting, resetting, and not setting of the vptr is handled automatically (by compiler) through code generated within each class constructor, destructor, and copy assignment operator. The type_info object associated with each class in support of runtime type identification (RTTI) is also addressed within the virtual table, usually within the table's first slot. Typeid(a pointer to derived class or base class) = static type of pointer, like typeid( p pointer to Derived ) == typeid( Base*), but typeid (*pointer) == typeid (Derived)
2. Figure illustrates the general C++ Object Model for our Point class. The primary strength of the C++ Object Model is its space and runtime efficiency. Its primary drawback is the need to recompile unmodified code that makes use of an object of a class for which there has been an addition, removal, or modification of the non-static class data members. (The two table model, for example, offers more flexibility by providing an additional level of indirection. But it does this at the cost of space and runtime efficiency. One table for data member, another for function member).
3. The class is its self friend, so in member function the parameter’s protected or private data can be accessed directly if type of parameter is class itself or its base class.
4. Friend function can not have access to static member of class without using object or class.
5. Only when one class has virtual function, the object of class has a virtual table, has type info (RTTI), otherwise all operator related to RTTI is resolved statically.
6. vptr is needed only for reference or pointer.
2. Object Lessons:
1. These are obviously not only very different styles of programming, but also very different ways of thinking about our programs. There are many more or less convincing arguments for why the data encapsulation of an ADT or class hierarchy is better (in the software engineering sense) than the procedural use of global data such as that in C programs.
2. The C++ implementations of a 3D point are more complicated than their C counterpart, particularly the template instances. Example: Template <class type, int dimension> class Point { … };
3. OOP doesn’t mean they are not also considerably more powerful or, again in software engineering sense, better. But being more powerful or better is not necessarily a convincing argument for OOL use.
4. Layout costs for adding encapsulation:
- non-static data member are directly contained within each class object.
- function members are not reflected in the object layout, only one copy of each non-inline member function is generated. Each inline function has either zero or one definition of itself each module in which it is used.
- the primary layout and access0-time within C++ are associated with the virtual, the virtual function mechanism in its support of an efficient run-time binding through a virtual pointer to a virtual function table of class,
- a virtual base class in its support a single, shared instance of a base class occurring multiple times within an inheritance hierarchy.
5. In cfront, for example, the two keywords class and struct are replaced by the shared token AGGR in the parser. The default access is public when using struct, but private when using class.
6. The real issue, however, is not whether all declarations of a user-defined type must use a consistent keyword. Rather the issue is whether the use of the class or struct keyword makes any promise as to the internal declaration of the type. That is, if use of the struct keyword enforces the C concept of a data abstraction, while use of the class keyword enforces the concept of an ADT, then, of course, failure to be consistent is an incorrect usage of the language.
7. The use of a one-element array at the end of a struct to allow individual struct object to address variable-sized arrays:
8. struct Dragon { char pc[1];}
9. struct Dragon *pmumb1 = ( struct Dragon * ) malloc(sizeof(struct Dragon)+strlen(string)+1);
10. The above example may or may not translate well when placed within a class declaration that: specifies multiple access sections containing data, derives from another class or is itself the object of derivation, defines one or more virtual functions, has a virtual base class.
11. The data members within a single access section are guaranteed within C++ to be laid out in the order of their declaration. The layout of data contained in multiple access sections, however, is left undefined.
12. Similarly, the layout of data members of the base and derived classes is left undefined, thereby also negating any guarantee that the trick might work.
13. Composition, rather than inheritance, is the only portable method of combining C and C++ portions of a class (the conversion operator provides a handy extraction method):
14. One reasonable use of the C struct in C++, then, is when you want to pass all or part of a complex class object to a C function. This struct declaration serves to encapsulate that data and guarantees a compatible C storage layout. This guarantee, however, is maintained only under composition. Under inheritance, the compiler decides whether additional data members are inserted within the base struct sub-object.
15. The C++ programming model directly supports three programming paradigms:
- The procedural model as programming in C, and supported within C++;
- The abstract data type model in which users of the abstraction are provided with a set of operations(the public interface), while the implementation remains hidden;
- The object-oriented (OO) model in which a collection of related types are encapsulated through an abstract base class providing a common interface.
16. Although you can manipulate a base class object of an inheritance hierarchy either directly or indirectly, only the indirect manipulation of the object through a pointer or reference supports the polymorphism necessary for OO programming.
17. The actual type of the object addressed is not resolved in principle until runtime at each particular point of execution. In C++, this is achieved only through the manipulation of objects through pointers and references. In contrast, in the ADT paradigm the programmer manipulates an instance of a fixed, singular type that is completely defined at the point of compilation.
18. In C++, polymorphism exists only within individual public class hierarchies.
19. The C++ language supports polymorphism in the following ways:
- through a set of implicit conversions, such as the conversion of a derived class pointer to a pointer of its public base type: BaseClass *p = new DerivedClass();
- Through the virtual function mechanism;
- Through the dynamic_cast and typeid operation.
20. The primary use of polymorphism is to effect type encapsulation through a shared interface usually defined within an abstract base class from which specific subtypes are derived. This not only allows for the addition, revision, or removal of type without requiring changes to user programs. It also frees the provider of a new subtype from having to recode behavior or actions common to all types in the hierarchy itself.
21. The memory requirements to represent a class object in general are the following:
- the accumulated size of its non-static data members;
- plus any padding (between members or on the aggregate boundary itself) due to alignment constraints (or simple efficiency);
- plus an internally generated overhead to support the virtual ( virtual pointer to vtbl).
- The size of empty class is actually 1, not 0 for a comparison ( &a==&b).
22. The memory requirement to represent a pointer, however, is a fixed sized regardless of the type it addresses, or to represent a reference; internally, a reference is generally implemented as a pointer and the object syntax transformed into the indirection required of a pointer.
23. A pointer and a reference support polymorphism because they do not involve any type-dependent commitment of resources. Rather, all that is altered is the interpretation of the size and composition of the memory they address.
24. Any attempt to alter the actual size of the object za, however, violates the contracted resource requirements of its definition.
25. When a base class object is directly initialized or assigned with a derived class object, the derived object is sliced to fit into the available memory resources of the base type. There is nothing of the derived type remaining. Polymorphism is not present, and an observant compiler can resolve an invocation of a virtual function through the object at compile time, thus by-passing the virtual mechanism. This can be a significant performance win if the virtual function is defined as inline. During this process, the vptr will be reset or event there is not vptr for object which is not reference or pointer to class type.
26. To summarize, polymorphism is a powerful design mechanism that allows for the encapsulation of related types behind an abstract public interface, such as our Library_materials hierarchy. The cost is an additional level of indirection, both in terms of memory acquisition and type resolution.
27. C++ supports polymorphism through class pointers and references. This style of programming is called object-oriented.
28. C++ also supports a concrete ADT style of programming now called object-based (OB) non-polymorphic data types, such as String class. It is called final or sealed class in Java or C#.
29. An OB design can be faster and more compact than an equivalent OO design. Faster because all function invocations are resolved at compile time and object construction need not set up the virtual mechanism, and more compact because each class object need not carry the additional overhead traditionally associated with the support of the virtual mechanism. However, an OB design also is less flexible.
C++ Knowledge series 1的更多相关文章
- Java Knowledge series 4
JVM & Bytecode Has-a or Is-a relationship(inheritance or composition) 如果想利用新类内部一个现有类的特性,而不想使用它的接 ...
- C++ Knowledge series Template & Class
Function Function is composed of name, parameter (operand, type of operand), return value, body with ...
- C++ Knowledge series Inheritance & RTTI & Exception Handling
Inheritance The pointer or reference to base class can address/be assigned with any of the classes d ...
- C++ Knowledge series Conversion & Constructor & Destructor
Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...
- C++ Knowledge series STL & Const
Thank to the pepole who devote theirself to the common libs. STL(http://www.cplusplus.com/reference/ ...
- Java Knowledge series 7
Pepole who make a greate contribution on common libaraies deserve our respect. Component(Widget) / S ...
- C++ Knowledge series 2
Programming language evolves always along with Compiler's evolvement The semantics of constructors O ...
- Java Knowledge series 5
Interface from user, not from implementor.(DIP) Interface-Oriented Programming. Interface or Abstrac ...
- Java Knowledge series 3
JVM & Bytecode Abstract & Object Object in Java (1) 所有东西都是对象object.可将对象想象成一种新型变量:它保存着数据,但可要求 ...
随机推荐
- Learn to See in the Dark(论文阅读笔记)
最近做项目看了一篇论文<Learn to See in the Dark>下面是一些论文笔记 概括: 这篇论文主要介绍的是在低光照的环境下用两个标准的FCN网络,通过控制变量法来对比不同的 ...
- G - Game HDU - 5242 (数链剖分)
题目链接: G - Game HDU - 5242 题目大意:首先是T组测试样例,给出一颗以1节点为根的树,每个节点有各自的价值,有m次从根节点出发向下走到叶子节点的机会,每次会得到所有经过节点的权值 ...
- HDU4641 || 6194多校 (后缀自动机-最少出现K次的字串个数 || 恰好出现K次字符串的个数)
http://acm.hdu.edu.cn/showproblem.php?pid=4641 http://acm.hdu.edu.cn/showproblem.php?pid=6194 题意: 开始 ...
- hdu 3709 Balanced Number(平衡数)--数位dp
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- Mac 10.12安装流量监控软件Magican
说明:Magican这家公司已经不维护了,但是软件是单机版的,可以正常使用,但是有些10.12的机器应该是无法看到每个进程的明细,总速度可以正常显示. 下载: (链接: https://pan.bai ...
- 使用redux-devtools工具
在vue中型项目开发的过程中,一般都是要用到vuex这个状态管理工具的,这样可以方便我们管理全局的状态,同时,为了在开发的过程中,更加方便地实时查看到state状态,我们会使用 vue-devtool ...
- h2数据库的简单使用
1.登录H2数据库的WebConsole控制台 2.设置数据库连接 3.连接测试通过之后,点击[连接]按钮,登录到test数据库的webConsole 4.创建表 复制H2数据库提供的样例SQL脚本, ...
- Editplus下载、安装并最佳配色方案(强烈推荐)
不多说,直接上干货! Editplus下载 第一步:进入官网 https://www.editplus.com/ 第二步:下载 https://www.editplus.com/download.ht ...
- javac的命令(-Xbootclasspath、-classpath与-sourcepath等)
当编译源文件时,编译器常常需要识别出类型的有关信息.对于源文件中使用.扩展或实现的每个类或接口,编译器都需要其类型信息.这包括在源文件中没有明确提及.但通过继承提供信息的类和接口. 例如,当扩展 ja ...
- 深度学习(十五) TextCNN理解
以下是阅读TextCNN后的理解 步骤: 1.先对句子进行分词,一般使用“jieba”库进行分词. 2.在原文中,用了6个卷积核对原词向量矩阵进行卷积. 3.6个卷积核大小:2个4*6.2个3*6和2 ...