Why can't I call a non-static member (property) in a static method (function) of a class?

Source: Internet
Author: User
The program will eventually be executed in memory, and the variable can be accessed only if it occupies a place in memory.
Static members of a class (variables and methods) belong to the class itself, which allocates memory when the class is loaded, can be accessed directly through the class name, and non-static members (variables and methods) are objects of the class, so memory is allocated only when the class's object is generated (an instance of the class is created) and then accessed through the object (instance)
Accessing its non-static members in a static member of a class is an error because a static member of the class already exists when the non-static member of the class does not exist, and accessing something that does not exist in memory is of course an error: Class ca{private:int A;//non-static members, Allocates memory when an instance of the class is created, different instances of the class correspond to different memory regions

Why can't I call a non-static member (property) in a static method (function) of a class?

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.