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的更多相关文章

  1. Java Knowledge series 4

    JVM & Bytecode Has-a or Is-a relationship(inheritance or composition) 如果想利用新类内部一个现有类的特性,而不想使用它的接 ...

  2. C++ Knowledge series Template & Class

    Function Function is composed of name, parameter (operand, type of operand), return value, body with ...

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

  4. C++ Knowledge series Conversion & Constructor & Destructor

    Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...

  5. C++ Knowledge series STL & Const

    Thank to the pepole who devote theirself to the common libs. STL(http://www.cplusplus.com/reference/ ...

  6. Java Knowledge series 7

    Pepole who make a greate contribution on common libaraies deserve our respect. Component(Widget) / S ...

  7. C++ Knowledge series 2

    Programming language evolves always along with Compiler's evolvement The semantics of constructors O ...

  8. Java Knowledge series 5

    Interface from user, not from implementor.(DIP) Interface-Oriented Programming. Interface or Abstrac ...

  9. Java Knowledge series 3

    JVM & Bytecode Abstract & Object Object in Java (1) 所有东西都是对象object.可将对象想象成一种新型变量:它保存着数据,但可要求 ...

随机推荐

  1. C#生成验证码类

    using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;us ...

  2. 【算法笔记】A1022 Digital Library

    题意 输入n本书的信息:id,书名,作者,关键字,出版社,出版年份.搜索图书,输出id. 思路 定义5个map<string, set<int> >,分别存放Title, Au ...

  3. 洛谷 P1273 有线电视网(树形背包)

    洛谷 P1273 有线电视网(树形背包) 干透一道题 题面:洛谷 P1273 本质就是个背包.这道题dp有点奇怪,最终答案并不是dp值,而是最后遍历寻找那个合法且最优的\(i\)作为答案.dp值存的是 ...

  4. JavaScript设计模式(一)

    什么是设计模式呢? 就是指对于类似的问题,我们可以用大致相同的思想.方法去解决之,而这种通用的思想.方法就是设计模式.学习设计模式可以帮助我们在遇到问题时迅速地搜索出一种清晰的思路来实现之. 第一部分 ...

  5. 本地jar包 安装到本地仓库中的命令

    maven 项目 本地jar包 安装到本地仓库中去: 首先进入到该文件所在文件夹内 若不在直接绝对路径就可以.注意命令中的空格 mvn install:install-file  -Dfile=文件名 ...

  6. Win10 VS2015 静态编译Qt5.6.2源码

    由于VS2015需要CRT等拓展组件,因此把内部编写的工具软件以静态发布,固需要编译Qt源码.Qt5.6.2版本,VS2015,Win10 1.安装python,perl,下载jom 2.改文件com ...

  7. 设置spacevim字体显示乱码问题

    https://github.com/powerline/fonts clone powerline fonts 仓库 执行项目中的 install.sh 安装字体 修改终端配置中使用的字体为 xxx ...

  8. 3D效果

    3D transform:rotate3d(x,y,z,a) (0.6,1,0.5,45deg) transform-origin 允许改变转换元素的位置,(中心点) transform-style ...

  9. Linux打开txt文件乱码解决方案

    在ubuntu16.04下打开dic_ec.txt,出现中文乱码. 先输入 gsettings set org.gnome.gedit.preferences.encodings auto-detec ...

  10. 关于PHP数据库mysql的一些案例

    案例1:查询select 使用php连接数据库class9, 获取数据库的表student中的信息, 然后输出到页面上(用表格套住) <?php header("Content-typ ...