Differences between struct and classes in C ++

Source: Internet
Author: User

Class and struct have only two differences in C ++. (1) The default access permission for members in class is private, while that for struct is public. (2) inherit from class is private by default, while inherit from struct is public by default. Difference between the member operator (·) and the operator pointing to the struct member (->)

Difference between the member operator (·) and the operator pointing to the struct member (->)

Both are used to reference struct variables, but their application environments are completely different. The former is used in a general struct variable, and the latter is used with pointers to struct variables. For example: defined

Struct student

{

Long num;

Float score;

};

Struct student stud, * PTR = & stud;

Then stud. num, stud. score, PTR-> num and so on are all correct references, but PTR. num and stud-> num are not allowed. In fact, PTR-> num is equivalent to (* PTR ). num, which is dedicated to providing the-> operator for the sake of being more intuitive.

It is pointed out that both of them have the highest priority and are combined from left to right.

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.