"Depth exploration of C ++ Object Model" Chapter 1 focus: on target

c ++ design object model: nonstatic the data members are placed in each class within, static the data members were placed in a class other than, static and nonstatic the function is placed in the class outside. virtual function to support the following two steps:

  1. Each class generating a Virtual Table ( the vtbl ), which point to each storage virtual function pointers.
  2. Each class object has a pointer to the vptr , relevant points the vtbl , the vptr set and reset by each class , and the destructor constructor copy assignment operator automatically. vtbl first one slot usually put is that this class of type_info object pointer to support Runtime Identification of the type , RTTI

static variables stored in the global variable region ( .data ), is in line with static semantics that need to be initialized outside the class, allocating memory, because its life cycle is global, and the class students object lifecycle only after it is defined Start.

 

c ++ in class and struct differences: struct can do class of all things, just struct default member access to public , class default member access to Private ; struct defaults to public inheritance, class defaults to private inheritance; when struct and class no member variables and constructor (no member function) are all public , it is possible by {} are initialized.

struct suitable as is a data aggregate; class suitable as an object.

c ++ in the following manner to support multi-state:

  1. Via a set of implicit conversion operations, for example, into a pointer to the derived class pointer to its base class :

    Shape *ps = new Circle();

  2. through the virtual function mechanism :

    ps -> rotate();

  3. via dynamic_cast and typeid operators

    if(Circle *pc = dynamic_cast<Circle*>(ps)) ...

typeid essentially that is, through the first virtual function table slot pointer is pointing within type_info object to determine what kind of class 

Polymorphic main purpose is to influence the type of package via a common interface, the interface is typically defined in the base class, and have different implementations in different derived class. Through the virtual function mechanism to determine which function at runtime instance call in the end.

void Rotate (X-datum, const X-pointer *, const X-& Reference) {
     // The following operations are always invoked X :: rotate (), because the type of datum has been determined not to take the virtual function table manner; 
    datum.rotate ( );
     // following two operations must know whether the call is in operation to that of the rotate () example 
    (* pointer) .rotate (); 
    reference.rotate (); 
} 
main () { 
    the Z Z; // the Z is X is a subtype 
    Rotate (Z, & Z, Z);
     return  0 ; 
}

Different types of pointers: only in their address out of the object types, "pointer types" will teach the compiler how to interpret the contents of a particular memory address and size. Of type void * pointer capable of holding only one address, but not its operation by referring to the Object .

Bear b;
ZooAnimal *pz = &b;
Bear *pb = &b;

pz and pb point Bear object of one byte , the difference between: pb address contains the entire covered Bear object , and pz address contains only covered Bear object of zooAnimal portion.

B Bear; 
the ZooAnimal ZA = B; // caused by cutting, in fact calls the copy constructor, setting correct the vptr 
za.rotate (); // call ZooAnimal :: rotate (), have been identified by the compiler

The reason why the support of a pointer or reference to polymorphism, because they do not lead to changes in the memory associated with the type, they point to change only memory "interpreted size and content."

 

Guess you like

Origin www.cnblogs.com/senshaw/p/10944439.html