C + + object Model (2) Variable access

Source: Internet
Author: User

How do I access members?

described earlier. The C + + object model, which describes the impact of the C + + object model on Access members. In fact , the C + + object model is clear, and the member access mechanism is clear. Here is an overview of how data members and function members are accessed, respectively.

Object Size Issues

among them: 3 a function in a class is a virtual function.

L Derived inherit Base

L derived_virtual virtual inheritance Base

void Test_size () {    Base B;    Derived D;    Derived_virtual DV;    cout << "sizeof (b): \ t" << sizeof (b) << Endl;    cout << "sizeof (d): \ t" << sizeof (d) << Endl;    cout << "sizeof (DV): \ t" << sizeof (DV) << Endl;}

The output is as follows:

becauseBasecontains virtual function table pointers, allsizeto be4;derived inherit base size 4 derived_virtual virtual inheritance base According to the previous model, Derived classes have their own virtual function tables and pointers, and have delimiters ( 0x00000000 4+4+4=12 .

#pragma onceclass empty{public:    Empty (void);    ~empty (void);};

Empty P ,What is the size ofsizeof (p) ? In fact, it's not empty, it has a cryptic 1byte, which is a char that is inserted into the compiler . This will make this class has a unique address within the two objects .

How data members are accessed (direct fetch)

associated with the actual object model , based on the object start address + offset is obtained.

static binding and dynamic binding

when the program calls the function, the executable code block is used? The compiler is responsible for answering this question. Resolving a function call in the source code to execute a specific function code block is called a function name binding ( binding in c c++ . The compiler must look at the function arguments and function names to determine which function to use. However, the compiler can complete this binding during the compilation process, which is called static binding,also known as early binding (early Binding) .

However the virtual function is this work becomes more difficult. Which function to use is not determined during the compile phase, because the compiler does not know which type the user will choose . Therefore, the compiler must be able to select the correct virtual function code when the program is run, which is called dynamic binding, also known as late binding ( Late binding).

The use of virtual functions is at a cost, There are some costs involved in memory and execution speed , including:

L Each object will increase in size to store virtual function table pointers;

L for each class, the compiler creates a virtual function address table;

L for each function call, you need to perform an additional action, which is to find the address in the virtual function table.

Although non-virtual functions are slightly more efficient than virtual functions, they do not have the ability of dynamic linking.

how function members are accessed (indirect fetch)

associated with the actual object model , the normal function ( nonstatic,static The function address is obtained directly from the compiled and linked results, and if the virtual function is based on the object model, the address of the virtual function is fetched, and then the function address is found in the virtual function table.

How is polymorphism implemented ? Multi-State implementation

polymorphism ( POLYMORPHISN c++ Span style= "font-family: Microsoft Jacob Black;" is implemented by virtual functions. Through the preceding model "see " There are rewritten single inheritance Span style= "FONT-FAMILY:CALIBRI;" > " " knowing that if there are virtual functions in a class, the compiler automatically generates a virtual function table that contains a pointer to the virtual function table. The ability to implement polymorphic : virtual functions are allowed to be overridden by derived classes, and in virtual function tables, the derived class functions are overwritten ( override ) base class functions . In addition to this, the must also invoke a method with a pointer or reference to the line to assign the derived class object to the base class object.

above 2 classes, base class base, derived class Derived contain the following 2 a method:

void print () const;

virtual void print_virtual () const;

this 2 the difference between a method is that one is a normal member function and one is a virtual function. Write the test code as follows:

void Test_polmorphisn () {    Base B;    Derived D;       b = D;    B.print ();    B.print_virtual ();     Base *p;    p = &d;    P->print ();    P->print_virtual ();}

  

according to the model, only p->print_virtual () to achieve the dynamic, the other 3 calls are methods that call the base class. The reasons are as follows:

L b.print (); b.print_virtual (); Polymorphism is not possible because it is called through a base class object, not a pointer or reference .

l p->print (); polymorphism is not possible because, Print The function is not declared as virtual (virtual), and the derived class also defines the the print function simply hides the base class's print function.

Why is it necessary to set a destructor to a virtual function

Destructors should be virtual functions unless the class is explicitly not a base class (not inherited by other classes). The destructor for the base class is declared as a virtual function to ensure that the destructor is called in the correct order when the derived object is disposed.

from the previous introduction of the C + + The object model knows that if a destructor is not defined as a virtual function, then the derived class will not override the destructor of the base class, and the destructor of the derived class will not be called when there is a polymorphic behavior ( There is a risk of a memory leak!). ).

For example, by New A derived class object, assigned to the base class pointer, and then Delete The base class pointer.

void Test_vitual_destructor () {    Base *p = new Derived ();    Delete p;}

If the destructor of the base class is not a virtual function:

Note that a destructor call for a derived class is missing . When you declare a destructor as a virtual function, the call is normal:

C + + object Model (2) Variable access

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.