C ++ learning notes: function calls at the inheritance level.

Source: Internet
Author: User

Function calls in the inheritance level can be classified by calling method:

1. Directly calling through an object:

1. Call non-virtual functions. As we all know, a derived class Object (Public inheritance) is composed of two parts: the base class object and the derived class's own part, if you use a derived class object to call a non-virtual function (assuming the function is public in the base class) nvfcn (), the compiler first looks for the function name in the derived class section: If you find it, type check is performed, and the check is correct --> call, incorrect --> error is reported. If no result is found, search in the base class .. And so on. For example:

Class baseclass {public: baseclass (){};~ Baseclass () {}; void nvfcn () {cout <"base class" <Endl ;}}; class derivedclass: Public baseclass {public: derivedclass (){}~ Derivedclass () {}; void nvfcn () {cout <"derived class" <Endl ;}};
Derivedclass DC; DC. nvfcn (); // The result is a derived class. // If the nvfcn () function in the derived class is changed to nvfcn (int A), an error is returned.

Why is an error reported? Shouldn't I find it in the base class? Because the scope of the derived class is within the scope of the base class (the parameter list of the function is different, so that different functions are distinguished. After compilation, they are automatically converted into two different function names with two unused addresses.) The functions of the derived class have blocked functions with the same name in the base class!

If the function in the derived class is commented out, the base class function is called.
2. The same is true for virtual functions.

2. Call through a base class pointer or reference:

1. Call a virtual function. Virtual members are dynamically bound by referencing base classes or calling pointers. The function that calls base classes or derived classes is determined at runtime (based on the actual object type ), it is not based on the pointer or referenced static type. (You do not know the method that will be called before running the program, but the called address will be dynamically calculated through the computing program at runtime ).

Class baseclass {public: baseclass (){};~ Baseclass () {}; virtual void vfcn () {cout <"base:" <Endl;} void nvfcn () {cout <"base class" <Endl ;}}; class derivedclass: Public baseclass {public: derivedclass (){}~ Derivedclass () {}; void vfcn () {cout <"derived:" <Endl;} void nvfcn () {cout <"derived class" <Endl ;}};

BaseClass bc;DerivedClass dc;BaseClass * bp = &dc;bp->vfcn();

The result is a derived class.

2. Non-virtual functions. Non-virtual functions alwaysDuring CompilationBased on the object, reference, or pointer type that calls the function, the non-virtual function of the object is called no matter what type of the actual object is at runtime.Will call the versions defined in the base class..

Baseclass BC; derivedclass DC; baseclass * BP = & DC; BP-> vfcn (); BP-> nvfcn (); // display the base class

(The above is a small summary After cainiao reads a book. I do not know if this is true or not. I would like to thank you for your criticism !! I am very grateful)

Related Article

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.