没有合适的资源?快使用搜索试试~ 我知道了~
首页深入理解C++对象模型
"《Inside the C++ Object Model》是Addison Wesley出版社的一部经典著作,专为深入理解C++对象模型的读者而准备。这本书深入探讨了支持C++面向对象编程的各种底层机制,包括构造函数的语义、临时对象的生成、封装的支持、继承以及‘虚’的概念——虚拟函数和虚拟继承。作者Lippman通过揭示关于C++开销和复杂性的一些误解和神话,帮助开发者更高效、更有信心地编写代码,同时指出了一些隐藏的成本和权衡。他还阐述了各种实现模型的起源,预测了它们可能的发展方向,并解释了为何会形成当前的模型。本书详细探讨了C++对象模型的语义含义,以及这个模型如何影响你的程序设计。"
在这本书中,作者深入剖析了以下几个关键知识点:
1. 构造函数的语义:构造函数在C++中是初始化对象的重要工具,它们在对象创建时被调用,确保对象以正确状态开始其生命周期。Lippman会详细讨论构造函数的工作原理,包括成员初始化列表、构造函数的链调用以及构造函数的异常处理。
2. 临时对象的生成:临时对象在表达式中产生,用于临时存储计算结果。理解它们的生命周期和销毁规则对于优化代码性能至关重要。Lippman会解释何时以及如何生成临时对象,以及如何避免不必要的临时对象拷贝。
3. 封装的支持:封装是面向对象编程的基础,C++通过访问控制(public, protected, private)来实现。Lippman将解释这些访问修饰符如何影响对象的行为,以及如何通过封装来保护数据的安全性和实现信息隐藏。
4. 继承:继承允许类之间共享属性和行为,是实现代码重用的关键机制。Lippman会讨论单继承和多继承的实现细节,以及继承带来的二义性问题和解决策略。
5. 虚函数和虚继承:虚拟函数使得动态绑定成为可能,增强了多态性。虚拟继承则解决了多继承时的“菱形问题”。Lippman会深入分析这两者的内存布局和运行时行为,以及它们对性能的影响。
6. 程序转换和性能影响:C++的对象模型对编译器生成的机器代码有直接影响。Lippman会探讨这些转换如何影响程序的执行效率,包括函数调用的开销、虚函数表的使用等。
7. C++对象模型的历史与未来:作者回顾了C++对象模型的发展历程,并对其未来可能的变化进行预测,帮助读者理解语言演进的背景和趋势。
通过阅读《Inside the C++ Object Model》,开发者可以更深入地理解C++的内部工作原理,从而编写出更高效、更健壮的代码,同时也能更好地应对潜在的性能挑战和设计决策。这本书对于任何希望提升C++编程技能的人来说都是宝贵的资源。

// ... etc ...
private:
float _x;
float _y;
float _z;
};
inline ostream&
operator<<( ostream &os, const Point3d &pt )
{
os << "( " << pt.x() << ", "
<< pt.y() << ", " << pt.z() << " )";
};
or as a two
-
or three
-
level class hierarchy:
class Point {
public:
Point( float x = 0.0 ) : _x( x ) {}
float x() { return _x; }
void x( float xval ) { _x = xval; }
// ...
protected:
float _x;
};
class Point2d : public Point {
public:
Point2d( float x = 0.0, float y = 0.0 )
: Point( x ), _y( y ) {}
float y() { return _y; }
void y( float yval ) { _y = yval; }
// ...
protected:
float _y;
};
class Point3d : public Point2d {
public:
Point3d( float x = 0.0, float y = 0.0, float z = 0.0 )
: Point2d( x, y ), _z( z ) {}
float z() { return _z; }
void z( float zval ) { _z = zval; }
// ...
protected:
float _z;
};
Moreover, either of these implementations may be parameterized, either by the type of the coordinate:
template < class type >
class Point3d
{
public:
Point3d( type x = 0.0,
type y = 0.0, type z = 0.0 )
: _x( x ), _y( y ), _z( z ) {}
type x() { return _x; }
void x( type xval ) { _x = xval; }
// ... etc ...
private:
type _x;

type _y;
type _z;
};
or by both the type and number of coordinates:
template < class type, int dim >
class Point
{
public:
Point();
Point( type coords[ dim ] ) {
for ( int index = 0; index < dim; index++ )
_coords[ index ] = coords[ index ];
}
type& operator[]( int index ) {
assert( index < dim && index >= 0 );
return _coords[ index ]; }
type operator[]( int index ) const
{ /* same as non-const instance */ }
// ... etc ...
private:
type _coords[ dim ];
};
inline
template < class type, int dim >
ostream&
operator<<( ostream &os, const Point< type, dim > &pt )
{
os << "( ";
for ( int ix = 0; ix < dim-1; ix++ )
os << pt[ ix ] << ", "
os << pt[ dim?];
os << " )";
}
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. Those arguments, however, are often lost on programmers who are charged with getting an application up
and running quickly
and
efficiently. The appeal of C is both its leanness and its relative simplicity.
The C++ implementations of a 3D point are more complicated than their C counterpart, particularly the template
instances. This doesn't mean they are not also considerably more powerful or, again in a software engineering sense,
better. But being more powerful or better is not necessarily a convincing argument for their use.
Ru
-
Brd

Ru
-
Brd
Layout Costs for Adding Encapsulation
An obvious first question a programmer might ask while looking at the transformed Point3d implementations under
C++ concerns the layout costs for adding encapsulation. The answer is that there are no additional layout costs for
supporting the class Point3d. The three coordinate data members are directly contained within each class object, as
they are in the C struct. The member functions, although included in the class declaration, are not reflected in the
object layout; one copy only of each non
-
inline member function is generated. Each inline function has either zero or
one definition of itself generated within each module in which it is used. The Point3d class has no space or runtime
penalty in supporting encapsulation. As you will see, the primary layout and access
-
time overheads within C++ are
associated with the virtuals, that is,
the virtual function mechanism in its support of an efficient run
-
time binding, and
a virtual base class in its support of a single, shared instance of a base class occurring multiple times within an
inheritance hierarchy.
There is also additional overhead under multiple inheritance in the conversion between a derived class and its second
or subsequent base class. In general, however, there is no inherent reason a program in C++ need be any larger or
slower than its equivalent C program.
Ru
-
Brd

Ru
-
Brd
1.1 The C++ Object Model
In C++, there are two flavors of class data members
梥
tatic and nonstatic
梐
nd three flavors of class member functions
梥
tatic, nonstatic, and virtual. Given the following declaration of a class Point:
class Point
{
public:
Point( float xval );
virtual ~Point();
float x() const;
static int PointCount();
protected:
virtual ostream&
print( ostream &os ) const;
float _x;
static int _point_count;
};
how is the class Point to be represented within the machine? That is, how do we model the various flavors of data and
function members?
A Simple Object Model
Our first object model is admittedly very simple. It might be used for a C++ implementation designed to minimize the
complexity of the compiler at the expense of space and runtime efficiency. In this simple model, an object is a sequence
of slots, where each slot points to a member. The members are assigned a slot in the order of their declarations. There
is a slot for each data or function member. This is illustrated in Figure 1.1
.
Figure 1.1. Simple Object Model
In this simple model, the members themselves are not placed within the object. Only pointers addressing the members
are placed within the object. Doing this avoids problems from members' being quite different types and requiring
different amounts (and sometimes different types of) storage. Members within an object are addressed by their slot's
index. For example,
_x
's index is 6 and
_point_count
's index is 7. The general size of a class object is the size of a
pointer multiplied by the number of members declared by the class.
Although this model is not used in practice, this simple concept of an index or slot number is the one that has been
developed into the C++ pointer
-
to
-
member concept (see [LIPP88]
).
A Table
-
driven Object Model
For an implementation to maintain a uniform representation for the objects of all classes, an alternative object model

For an implementation to maintain a uniform representation for the objects of all classes, an alternative object model
might factor out all member specific information, placing it in a data member and member function pair of tables. The
class object contains the pointers to the two member tables. The member function table is a sequence of slots, with
each slot addressing a member. The data member table directly holds the data. This is shown in Figure 1.2 (on page
8).
Figure 1.2. Member Table Object Model
Although this model is not used in practice within C++, the concept of a member function table has been the traditional
implementation supporting efficient runtime resolution of virtual functions.
[1]
[1]
At least one implementation of the CORBA ORB has used a form of this two table model. The SOM
object model also relies on this two table model
[HAM95]
.
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. Nonstatic data members are allocated directly within each class object. Static data members
are stored outside the individual class object. Static and nonstatic function members are also hoisted outside the class
object. Virtual functions are supported in two steps:
1.
A table of pointers to virtual functions is generated for each class (this is called the
virtual table
).
2.
A single pointer to the associated virtual table is inserted within each class object (traditionally, this has been
called the
vptr
). The setting, resetting, and not setting of the vptr is handled automatically through code
generated within each class constructor, destructor, and copy assignment operator (this is discussed in Chapter
5
). 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.
Figure 1.3
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 nonstatic 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.)
Figure 1.3. C++ Object Model
剩余188页未读,继续阅读
相关推荐






Li_Shugan1
- 粉丝: 176
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- Qt C++实现AMP-204C运动控制卡开发教程
- Jupyter环境下网络抓取挑战解析
- HyperLynx IBIS模型仿真与AM335x PCB设计文件
- 深度学习实现车牌识别技术与应用
- 智能化教育工具:随机出卷系统介绍
- StructureMap:.NET依赖注入容器的实践与应用
- Rosetta软件:粗糙集方法的数据处理利器
- 易语言源码:如何修改进程占用内存
- LabVIEW视觉开发模块2016年F1补丁安装指南
- 探索Android拼图游戏的算法及图片压缩技术
- K-NN癌症诊断分类器的机器学习算法实现与优化
- CExpert单边交易EA开发:结合MACD策略示例
- Android手机天气预报软件的毕业设计开发
- KubeToolbox:Kubernetes集群部署与CI优化工具
- Lingo 13:高效解决各类优化问题的工具介绍
- MT4超级屏幕截图指标1.0:自动定时截图与邮件分享
安全验证
文档复制为VIP权益,开通VIP直接复制

扫码关注,限时领取CSDN余额