The static member function indirectly accesses non-static data members.

Source: Internet
Author: User
// The static member function indirectly accesses non-static data members.
// Static member functions can directly reference static data members and static member functions of the class, but cannot
// Directly reference non-static data members and non-static member functions; otherwise, an error is returned during compilation.
// If You Want To reference an object, you must pass the parameter to obtain the object name, and then use the object name to reference it.
# Include <iostream>
Using namespace STD;

Classmyclass
{
PRIVATE:
Intm; // non-static data member
Staticintn; // static data member
Public:
Myclass (); // Constructor
Staticintgetn (myclass A); // static member function
};

Myclass: myclass ()
{
M = 10;
}

Intmyclass: getn (myclass)
{
Cout <a. m <Endl; // indirectly uses non-static data members through the class
Return N; // directly use static data members
}

Intmyclass: N = 100; // static data member Initialization

Voidmain ()
{
Myclass app1;
Cout <app1.getn (app1) <Endl; // reference a static function member using an object
Cout <myclass: getn (app1) <Endl; // reference a static function member using the class name
}

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.