Key concepts in C ++: Name Search and inheritance

Source: Internet
Author: User
Key concepts: Name Search and inheritance

The key to understanding the hierarchy of inheritance in C ++ is to understand how to determine function calls. To determine whether a function is called, follow these four steps:

1)First, determine the static type of the object, reference, or pointer for the function call.

2)Find the function in this class. If the function cannot be found, search for the function in the base class. Follow the inheritance chain of the class until the function is found or the last class is searched. If the name cannot be found in the class or its related base class, the call is incorrect.

3)Once the name is found, perform a regular type check to check whether the function call is valid if the defined value is specified.

4)Assuming that the function call is valid, the compiler generatesCode. If a function is a virtual function and is called by reference or pointer, the compiler generates code to determine which function version to run based on the dynamic type of the object. Otherwise, the compiler generates code to directly call the function.

Class A {protected: int m_data; public: A (int data = 0) {m_data = data;} int getdata () {return dogetdata ();} virtual int dogetdata () {return m_data ;}}; Class B: Public A {protected: int m_data; public: B (INT DATA = 1) {m_data = data;} int dogetdata () {return m_data ;}}; Class C: Public B {protected: int m_data; public: C (INT DATA = 2) {m_data = data ;}}; class base {public: virtual int fun () {cout <"base: Virtual int fun ()" <E NDL; return 0 ;}}; class D1: public base {public: int fun (INT) {cout <"D1: int fun (INT)" <Endl; return 0;} int myfun (INT, INT) {cout <"Two int" <Endl; return 0 ;}; class D2: Public D1 {public: int fun (INT) {cout <"D2: int fun (INT)" <Endl; return 0;} int fun () {cout <"D2: int fun () "<Endl; return 0;} int myfun (INT, Int, INT) {cout <" Three int "<Endl; return 0 ;}}; int main () {C (10); cout <C. getdat A () <Endl; cout <C. a: getdata () <Endl; cout <C. b: getdata () <Endl; cout <C. c: getdata () <Endl; cout <C. dogetdata () <Endl; cout <C. a: dogetdata () <Endl; cout <C. b: dogetdata () <Endl; cout <C. c: dogetdata () <Endl; d2 D2; // d2.myfun (2, 2); // "D2: myfun": The function does not accept two parameters. Only functions defined in the same class can be reloaded, but not inherited functions. D2.myfun (2, 2); getchar (); Return 0 ;}

Running result:

Note:

1) the overload of member functions can only happen to several functions defined in the same class. Functions inherited from the parent class cannot be overloaded. Because when you use a subclass object to call the "overload function,Name Search rulesFirst in the subclass, the function with this name is found and will not continue searching for the parent class, so there is no need to reload the function. (This problem is described in the remarks Section)

2) Because D1 defines an int fun (INT), It shields the virtual int fun () function of the base class (). The virtual function inherited from base cannot be called through the D1 object (or D1 reference or pointer) because the virtual function is blocked,Name Search rulesIt determines that it finds the fun function in D1 and does not continue to look up.

3) for the output of the previous 11111011, only the first one is explained, and the following is similar to: cout <C. getdata () <Endl;

It was originally intended to call the getdata () of Class C, and is not defined in Class C, so the getdata () of Class B is called, but is also not defined in Class B, so the getdata () of Class A is called because the dogetdata () of Class () it is a virtual function. Therefore, dogetdata () in Class B is called, while dogetdata () in Class B returns B: m_data, so 1 is output.

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.