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

    技术2022-05-11  56

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

    2. C++ Object ModelHow Compiler implement the OO:A Simple Object ModelA Table-driven Object ModelTwo conceptions: virtual table vtbl;vptr

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

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

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

    4.  An Object DistinctionC++ 直接支持三种programming paradigms (what's paradigm)1.procedural model2.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 slicedthink.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 apointer or a reference, the manipulation of a pointer or reference in C++ does not in itself necessarily resultin 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 ofits 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)