What are the differences between struct and class in C ++ ?, Differencesstruct

Source: Internet
Author: User

What are the differences between struct and class in C ++ ?, Differencesstruct

Question:

This question was already asked in the context of C #/. Net.

Now I 'd like to learn the differences between a struct and a class in C ++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design.

I'll start with an obvious difference:

  • If you don't specifypublic:Orprivate:, Members of a struct are public by default; members of a class are private by default.

I'm sure there are other differences to be found in the obscure corners of the C ++ specification.


Answer:

You forget the tricky 2nd difference between classes and structs.

Quoth the standard (11.2.2 ):

In absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class.

And just for completeness 'sake, the more widely known difference between class and struct is defined in (11.2 ):

Member of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or union are public by default.


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.