One: Combination: Embed an object of a type x as a common object to the middle of the new type y. This is very meaningful: 1. He means that X can be part of Y's internal implementation. For new categories, it is not necessary to write an interface of the analog X of this embedded object, because the direct call method operation X object is directly valid. Easy. There is no need to simulate X method to set a member, which is not clear. For example, CLASS CPU {public: CPU (); ~ cpu (); void cacultor (); void cacultor (); void isok (); ... private: ...}; // computer can not inherit CPU, CPU is just its independent part of Class Computer {public: computer (); ~ computer (); void Docaculator () {mycpu.cacultor ();}; // Operation CPU object uses its method ... private: cpu mycpu;}; 2 use objects embedded The constructor can eliminate some implicit type conversions. Of course, you can also explicitly declare the constructor using Explicit to avoid him "unconsciously invigible to become C perfect support custom type sacrifice." For example: Class Example {public: ... EXAMPLE (INT I): M_member (i) {}; Friend Bool Operator == (Example A, Example B); Bool Dosomething (Example & A) {Return (A == m_member); // Although there is no match == operation type, but the compiler // will find the constructor to convert int to the form of objects that can be compared, it is going to do, whether you may be due to hand mistakes or What / / The result can be seen ^ _ ^}; ... private: int m_member;}; for the above implicit conversion problem, you can use Explicit to eliminate, you can also use the inner unit variable object constructor To reduce the probability of transformation of automatic herbs. Just say that the multi-structure function does not bring a problem with the transformation of the hermit. 3 About Nested Use Some Expansion: About Nested Declaration: In addition to defining classes, define their objects or pointer type variables in other classes, Class A {Class B; B * P; ...};
Class A :: b {// Definition, such a similar function body definition can reduce the compilation information of the header file // If you define B directly in the header file, it is large.
2: 1 Inheritance: Inherited becomes a parent class, inheriting the class class of the parent class. Subclasses all methods and members of the parent class. Syntax use subclass [colon] [inherit type] parent class, inherit type: 1. Public inheritance public does not say 2. Private inheritance private: For syntax integrity. And a combination of methods for processing the object-containing method is a bit similar. Method / Class Dad {public: void fun1 (); void fun2 (); private: ...}; Class Son: DAD {public:
Dad :: fun1 (); // Need to declare to use DAD :: FUN2 (); // If you call the parent class directly in the subclass, there is a problem}; 3. Protection inheritance: protect is A relatively important syntax in C , usage than private easing, allows methods of using parent classes in subclasses. 2 Abstract class: Class containing pure virtual functions is called abstract classes, Virtual fun () = 0; abstract classes cannot be firm.. It is only used to abstract a layer of method description and does not have to be implemented, so there is no meaning of the instantification. If we don't want to appear in the class, we can postpone the description knowing a derived class using an abstract class. For example: Describe the collection of T * Pointer Template
Void Tune (Instrunce & Object) {Object.Play ();} If you call void main () {Wind Object1; Tune (Object1); // We can get the Play method in WIND instead of Instructure. } How do this do this function? Please see the comments in the Play method in WIND before discussing. You should notice that the Play method in the subclass is not declared as the virtual use as the virtual function and can you find this method? 1 Depending on the principle of overlay: This is a process method for the use of C designs. The method of all the Virtual methods in the base class can be covered by the method of the subclass of the same name, and does not change the Virtual property. This is actually useful assumption There is no principle, then the Virtual Void Play in Instrunce will not be overwritten by the Void Play () method in the subclass, turn it in the subclass of: 1. From Instrunce Inherited Virtual Void Play () Method 2. The Void Play () method defined by itself After running Object1 during the function call, find the virtual table in the WIND class and its method in the Wind class after the Object1 during the function call. Looking for a corresponding function, but unfortunately, because this function is not covered by Play in WIND, the call will never find what you expect in Wind, unless you explicitly declare to Virtual. 2 Receive a relaxation coverage principle to mention another relaxation parameter principle generally remember that the derived class can be used everywhere. Is a supplement to the above
Back to the topic: Why is the INSTRUMENT type of Wind object to find your own type of method? 1 First of all, all the categories of the Virtual function will generate a virtual table vtbl. As the names, VTBL is usually a group that stores those function pointers declared as Virtual (some compiler will process as a list form, but the purpose Yes, the entry of the pointer is the number of all Virtual functions in the Class (including Virtual from the base class but does not include the false parsive function of the base class - remember that the constructors and destructor cannot be inherited. The virtual machine does not work on it) Class Base {public: Virtual ~ base (); Virtual Void F1 (); Virtual Void F2 (); Virtual Void F3 ();
}; Then the function pointer in the virtue in Base is distributed as follows's vtbl: 1 ------------------------ à Base :: ~ base 2 ---------------------------> Base :: f1 () 3 ------------- ------------- à Base :: F2 () 4 ------------------------- à base: : f3 () If there is DeriveD inherited in the inherited VTBL distribution: All virtual functions pointer positions follow the declarations to the first time to the first virtual function location, then covered by Derived, then slam from the class Inherited the virtual function (the position of the overlay is unchanged) and then pat the virtual function behind. Class Derived: public base {public: Virtual ~ derived (); Virtual Void F0 (); Virtual Void F2 (); Virtual Void F4 ();
}
So in Derived, you will put it below 1 ---------------------------- à derived :: ~ derived () 2 ---- ------------------------ à derived :: f0 () 3 ------------------ ---------- à Base :: F1 () // Base class Virtual function 4 ----------------------- --- à derived :: f2 () // Override 5 -------------------------- à Base :: F3 ( 6 ---------------------------- à Derived :: f4 () Each class will generate a virtual function Zhang Zhen Table, if many such types of VTBL will use a lot of resources. How do so many vtbls do we handle its placement location? I remember that it is not clear that it is a probe or exploration method: Put the VTBL in the position of the declaration of the intrinsic, non-purely virtual function. But if you declare the virtual function as the inline, all virtual functions declare that this method will fail, what should I do? In order to ensure this method, the compiler will ignore whether the intrinsic flag of your declaration is inlined, and one is not inlined.
2 Of course, only saving a virtual table for you to do the entire search job, we need a thing that can find the virtual table, it is not useful, we must find it. So a special member is hidden in the class of virtual function declaration, he is a pointer VTPR pointing to the virtual table. This pointer typically begins to the first point of VTBL. With this pointer, we can easily pass the virtual function in the virtual table. Easy to find a corresponding function in the middle offset. Base object:
Base :: ~ base () base :: ~ f1 () // vptr offset (F1 in VTBL offset) ....
Base * p-> f1 () Call is converted to (Base * P-> VPTR [I]) (P) // This is obtained here, and P is regarded as this // enclosure function. Easy to find the virtual table in the current object
3 So we seem to have completed the virtual function of the ideal, as if there is no problem. But if this object is inherited from Base to DeriveD type? How do you find the virtual table in the correct class when used as a base class? This requires VTBL to add some other things to help runtime identify the object type Type_info.
Object Type Information Save: For a class of each virtual function, a class object (comparison 拗) that should be class type types, which is not necessarily unique, of course, good implementation may uniquely generate Type_info objects and only To generate a Type_info object for those actually used to RTTI, after the object type information object is placed in VTBL, Strustup retains two spaces when designing the virtual table, and this time is sent. It is just that you can use these types of object information. ^ _ ^. At this time, the object layout is simple as follows:
VTBL At this time we can compare the concept of the two words. Polymorphism: VTPR VTBL TYPE_ID (one image of Type_info object type) ensures that the compiler is correctly found method of the object. In general: as long as there is a virtual function, the three stuffs above will be constructed, find the corresponding correct VTBL with VPTR and Type_info, then the offset of the VTPR method get the address, pass it to the current The pointer this can be called correctly. 04.10.2 2:00 PM