Inside The C++ Object Model ---- Object Lessons

1. Layout Cost for Adding Encapsulation
inheritance don't have cost. But virtual function and virtual base class will add extra cost.

2. C++ Object Model
How Compiler implement the OO:
A Simple Object Model
A Table-driven Object Model
Two conceptions: virtual table vtbl;
vptr

How the Object Model Effects Programs
X foobar(){
    X xx;
    X *px = new X;

    //foo() is virtual fucntion
    xx.foo();
    px->foo();
    delete px;
    return xx;
};

3.  A Keyword Distinction
template <struct Type>
template <class Type>
struct mumble { ... };


4.  An Object Distinction
C++ 直接支持三种programming paradigms (what's paradigm)
1.procedural model
2.abstract data type model
 String girl = "Anna"
 String daughter;
 daughter = girl;
 if (daughter == girl){
  ...
 }

3.object-oriented model
 void check_in(Library_materials *pmat){
  if (pmat->late())
   pmat->check_in();
  if (Lender *plend = pmat->reserved())
   pmat->notify( plend );
 }

PROBLEM:

Book book;
thing1=book;
//book is sliced
think.check_in();

Library_materials &thing2 = book;
thing.check_in();

think1 = book 是ADT paradigm 的一种行为,用于OO就乱了
Although the polymorphic manipulation of an object requires that the object be accessed either through a
pointer or a reference, the manipulation of a pointer or reference in C++ does not in itself necessarily result
in polymorphism!

The C++ language supports polymorphism in the following ways:
1. Through a set of implicit conversions, such as the conversion of a derived class pointer to a pointer of
its public base type:
 shape *ps = new circle();
2. Through the virtual function mechanism:
 ps->rotate();
3. Through the dynamic_cast and typeid operators:
 if ( circle *pc =
  dynamic_cast< circle* >( ps )) ... 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值