Inside The C++ Object Model(一)

============================================================================
1-1. 关键字struct和class的一点区别:
(1)struct内部默认public属性,class内部成员默认private属性;
(2)template中只能使用class来抽象类型参数,不能使用struct,template并不打算和C兼容。

1-2. C程序员有时会把单一元素的数组放在一个struct的尾端,于是每个struct objects可以拥有可变大小的数组。

struct memble{
/* */
char pc[1];
};

在这里,如果我们改用class来声明,则可能不行。C++中凡处于同一个access section的数据,必定保证以其声明次序出现在内存布局当中。然而被放置在多个access sections(public,protected,private)中的各笔数据,排列次序就不一定了。同样道理,base classes和derived classes的data members的布局也没有谁先谁后的强制规定,因而也就不能保证前述的C伎俩一定有效。Virtual functions的存在也会使前述伎俩的有效性成为一个问号。最好的忠告就是:不要那么做。

class stumble{
public:
// operations ...
protected:
// protected stuff
private:
/*private stuff*/
char pc[1];
};

注:组合,而非继承,才是把C和C++结合在一起的唯一可行方法。C struct在C++中的一个合理用途,是当你要传递“一个复杂的class object的全部或部分”到某个C函数中去时,struct声明可以将数据封装起来,并保证拥有与C兼容的空间布局。然而这项保证只在组合的情况下才存在。如果是“继承”而不是“组合”,编译器会决定是否应该有额外的data members被安插到base struct subobject之中。

============================================================================
1-3. 你可以直接或间接处理继承体系中的一个base class object,但只有通过pointer或reference的间接处理,才支持OO程序设计所需要的多态性质。如下:

// 描述objects:不确定类型
Library_materials *px = retrieve_some_material();
Library_materials &rx = *px;
// 描述已知物:不可能有令人惊讶的结果产生
Library_materials dx = *px;

你没有办法确定地说出px或rx到底指向何种类型的objects,你只能够说它要不就是Library_materials object,要不就是后者的一个子类型(subtype)。不过,我们倒是可以确定,dx只能是Library_materials class的一个objects。


============================================================================
1-4. C++对象模型(内存布局)

============================================================================

转载于:https://www.cnblogs.com/yyxt/p/5995027.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Inside the C++ Object Model focuses on the underlying mechanisms that support object-oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"--virtual functions and virtual inheritance. This book shows how your understanding the underlying implementation models can help you code more efficiently and with greater confidence. Lippman dispells the misinformation and myths about the overhead and complexity associated with C++, while pointing out areas in which costs and trade offs, sometimes hidden, do exist. He then explains how the various implementation models arose, points out areas in which they are likely to evolve, and why they are what they are. He covers the semantic implications of the C++ object model and how that model affects your programs. Highlights *Explores the program behavior implicit in the C++ Object Model's support of object-oriented programming. *Explains the basic implementation of the object-oriented features and the trade offs implicit in those features. *Examines the impact on performance in terms of program transformation.* Provides abundant program examples, diagrams, and performance measurements to relate object-oriented concepts to the underlying object model. If you are a C++ programmer who desires a fuller understanding of what is going on "under the hood," then Inside the C++ Object Model is for you! Get a value-added service! Try out all the examples from this book at www.codesaw.com. CodeSaw is a free online learning tool that allows you to experiment with live code from your book right in your browser.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值