Overloading, hiding, virtual functions, polymorphism analysis in C + +

Source: Internet
Author: User

It was not until today that I realized that I had been wrong about overloading for a long time. Thanks to the correct now, it is really horrible and shocking. All along, I think that overloading can occur between a base class and a derived class, for example:

1 classA {2  Public:3          voidTestint); 4 };5 classD | PublicA {6  Public:7          voidTestint,int);8 };9 Ten voidMain () One { A b b; -         -B.test (5); Error, should b.   A::test (5);  the}

I've always thought that when Class B inherits from the test function in Class A, in Class B, the test function of Class A and the test function defined by Class B are overloaded (because I think the two functions are separated by the number of parameters), and I think the 14th row calls the test function of Class A. Very shocking. Now summarize the heavy and hidden considerations for the wrong people to refer to:

Overload:

Within a class, if there are several functions with the same name, and the functions can be distinguished by the number of parameters or parameter types (note that they cannot be distinguished by the return type of a function), these functions are overloaded with each other. At this point, when you invoke these functions through a class object, the compiler can match the corresponding function by the number or type of arguments you pass, without ambiguity. This is where overloaded functions work (allowing you to use several functions with the same name). It is important to note that:

1. Overloads never occur between a base class and a derived class, as shown in the previous example. When a function of the same name exists in a base class and a derived class, a function of the same name in the derived class hides the function with the same name in the base class, regardless of the number of parameters or the same type of the function with the same name, so they are hidden, not overloaded. About hiding, which will be mentioned behind. In this case, the 14 rows of the example above will be error-free at compile time, suggesting that Class B does not have a function of type test (int).

2. In the same class, the overloaded functions must be distinguished by the number of formal parameters or parameter types, and cannot depend on the return type. That is, if two of the same name function parameters in the same class have exactly the same number and type, but the return value type is different, then the compilation will error, because when you invoke the same name function from the class object, the compiler will have two semantics and not know which function to choose. Remember, overloads must be distinguished by formal parameters.

3. In the same class, virtual and virtual functions, virtual functions and ordinary functions can also be overloaded between, the rules are exactly the same as above. The virtual function is mentioned below.

Hide:

Hiding can only appear between a base class and a derived class, not within the same class (such as 2 above, only causes the compiler to appear ambiguity). When a function with the same name exists in a base class and a derived class, the function with the same name in the derived class hides the function with the same name in the base class, not the overloaded relationship, regardless of the number of parameters of the function with the same name or whether the type is the same. At this point, when you call the function with the same name through a derived class object, you can only access the function of the derived class, and if you are hard to access the function of the base class, you need to precede the function name with the class scope, as shown in the upper code.

virtual function:

In a class, a function declared with the virtual keyword is a virtual function. The only purpose of a virtual function is to implement polymorphism (dynamic binding/runtime binding). About polymorphism, which is mentioned later. Virtual functions can only play a virtual attribute between a base class and a derived class (that is, to play the true purpose of a virtual function). In the same class, all virtual functions are the same as normal functions, using the same overloaded rules (as mentioned in the 3rd overload). As a result, virtual functions can be considered as ordinary functions in the same class (because their virtual properties do not work out), and the use of methods and considerations are identical to ordinary functions.

Polymorphic:

Polymorphism is the essence of object-oriented thinking. Plainly, when a member function is called through a base-class pointer or reference, the member function is not defined in the derived class until the run-time stage is determined. A little abstract, huh? Okay, let's look at some code.

1 classA {2  Public:3         Virtual voidTestint); 4 };5 6 classD | PublicA {7  Public:8         voidTestint);9 };Ten  One classE | PublicA { A  Public: -         voidTestint); - }; the  - voidMain () - { -AA0; +A &A1 =b; -A &A2 =C; + b b; A c C; at  -a0 = &b; -A0.test (2);//Call the test function of Class B -           -a0 = &C; -A0.test (3);//Call the test function of Class C in  -A1.test (4);//Call the test function of Class B toA2.test (5);//Call the test function of Class C +}

Let's first say what is polymorphic, and then say the conditions that produce polymorphism. Line 18th, a pointer variable a0 to class A type is defined in the main function, and the 21st and 22 lines define the object b,c of the derived class B and C, respectively. Line 24th assigns the pointer of object B to A0, line 25th A0.test calls the test function of Class B, line 27th, assigns the pointer to the object c to A0, and the 28th row A0.test calls the test function of Class C. This is polymorphic, is there a feeling? To be blunt, it is the method by which the derived class object can be called when the base-class pointer variable points to the derived class object. Similarly, references can also be implemented in Polymorphic, 第19-20, 30-31-line display.

The conditions for polymorphism are summarized below:

Which member functions want to be executed in polymorphic form, then these functions must:

1. Declare these member functions as virtual functions in the base class and implement them (must be implemented).

2. These member functions are also declared and implemented in derived classes (which must be implemented), and the functions of the base and derived classes must have the same name, with the number and type of parameters, and the return value type must be exactly the same as those in the base class. At this point, these functions in derived classes are automatically falsified, whether or not they are declared with virtual.

3. Assign a derived class object to a pointer variable or reference to the base class. At this point, a polymorphic implementation can invoke a method of a derived class (a method that conforms to a polymorphic condition rather than a normal method) using a base class pointer or reference.

In order to realize polymorphism, the three conditions of realizing polymorphism must be fully satisfied, and virtual functions of virtual function can be played out. In the absence of any condition, the virtual function of virtual functions will be broken, unable to achieve polymorphism. Virtual functions are broken by the virtual function is the same as the normal function, so the only use of virtual function is to achieve polymorphism.

Here are some examples of not polymorphic:

A. The base class is declared as a virtual function, also declared as a virtual function in a derived class, and also has the same name. However, the parameter types or the number of formal parameters of the function in a derived class are not the same as in the base class. At this point, the polymorphism is not satisfied, the two virtual functions of the derived class and the base class are only hidden relationships, there is no virtual attribute.

B. The base class is declared as a virtual function, and a function with the same name is also declared in the derived class, but not with virtual, and the parameter type or the number of parameters and the base class are not the same. The virtual function in the base class also loses the virtual attribute, and the function of the derived class is not to be falsified, and of course it is not polymorphic, and the two functions are simply hidden relationships.

C. A function in a base class is not a virtual function, declared as a virtual function in a derived class, with the same name, which is also not polymorphic, and the virtual functions of derived classes have no virtual attributes, they are also hidden relationships.

D. The two functions of the base class and the derived class have the same name, both are virtual functions, the number and type of formal parameters are the same, but the return value type is different, compile will error, because two virtual functions are hidden, the return value type conflicts, so hide an error. Note that if these two functions are not virtual functions, this will not be an error, the hide will succeed, and if the derived class is a virtual function, the base class is not a virtual function, but the error, hidden is also successful. This also indicates that the return value type must remain the same when it is virtualized and hidden.

Overloading, hiding, virtual functions, polymorphism analysis in C + +

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.