Calling member functions using class scope Operators

Source: Internet
Author: User

 
# Include "stdafx. H "# include <iostream> using namespace STD; class base {public: int func () {return 100;} private :}; class derived: public base {public: int func () {int TEM; TEM = base: func (); // You can explicitly call the methods in the base class (otherwise, infinitely recursive calls) cout <tem <Endl; return 0 ;}}; int main () {derived OBJ; obj. func (); int TEM; TEM = base: func (); // cout <tem <Endl; System ("pause "); return 0 ;}


The first call in the code above is correct, and the second call is wrong. Why?

Although the first call does not seem to use an object, in fact, the call object exists here, because the subclass object contains the parent class object, the call here can be equivalent(Base *) This-> func ();

The second call is obviously not the same as the call object.


Summary:

Class member functions must be called with explicit or implicit call objects;

class member functions are not the same as common functions,

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.