C + + overloads, overlays, hidden issues

Source: Internet
Author: User
Tags ranges

One, the member function is overloaded characteristic:
(1) the same range (in the same class); (2) The function has the same name; (3) the parameters are different;

(4) The virtual keyword is optional.

Second, the characteristics of coverage:

(1) different ranges (base classes and derived classes)

(2) The function has the same name;

(3) the same parameters;

(4) Thevirtual keyword must have (base class);

Third, the hidden characteristics:

(1) different ranges (base classes and derived classes)

(2) The function has the same name;

(3) Same parameters && no virtual keywords (base class);

(4) Different parameters, virtual keyword is optional;

What characteristics does the following code match?

1 classA {2  Public:3     Virtual voidFun (intB =Ten){4cout <<"A Fun:"<< B <<Endl;5     }6 };7 8 classD | PublicA {9  Public:Ten     voidFun (intB = -){ Onecout <<"B Fun:"<< B <<Endl; A     } - }; -  the intMain () - { - b b; -A &a =b; + A.fun (); -}

The output here is B fun:10; This answer makes me think for a long time, B fun is due to the coverage mechanism but 10 how to get it? I reverse the EXE found that the 10 at compile time has been determined, then what is the mechanism to let the compiler determine this parameter value is 10? Rather than 20. A &a actually calls the function itself within a, so the function argument comes from the default parameter of a, int B = 10; You may have questions: (learning is to think more)

What characteristics does the following code match:

1 classA {2  Public:3     voidFun (intB =Ten){4cout <<"A Fun:"<< B <<Endl;5     }6 };7 8 classD | PublicA {9  Public:Ten     voidFun (intB = -){ Onecout <<"B Fun:"<< B <<Endl; A     } - }; -  the intMain () - { - b b; -A &a =b; + A.fun (); -}
View Code

Might think it is the same code, look closely will find that the base class of virtual disappeared; Here the output is a fun:10; here a points to the address of "a" and calls the fun function;

Thinking of a strong turn problem (to be mended);

C + + overloads, overlays, hidden issues

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.