Inside The C++ Object Model ---- Object Lessons
Posted on 2007-02-19 22:08 Teddy Yan 阅读(134) 评论(0) 编辑 收藏 举报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 )) ...