In C + +, what is called class, struct, union?

Source: Internet
Author: User

In C + +, class and struct are inherently interoperable
The members of class are private by default, and members of the struct are public by default
But the general habit of hiding member variables in class declaration, member variables can be publicly accessible and relatively simple with the struct declaration (personal understanding is that the struct is from C, in C is so used)

As for union, no matter how many members it has inside, at any moment, only one is available, because all members have a common address
For example
Union A
{
int A;
float F;
Struct St St;
};
The three variables in the a,f,st address are the same, that is, together,
But the size of sizeof (A) is the size of one of the largest variables (regardless of the byte pair)
This is because three variables are at the same address, so the size must be able to satisfy any one of the variables used, so you must use the largest one.

Union is similar to a struct in many places, but it can also encapsulate data functions, but there are some special places. 1. Only one member has a value at a point in time (in other words, all members use the same piece of memory). 2, can not do the base class, there is no virtual function. 3. Cannot have static and reference members. 4. You cannot nest classes (you cannot actually have members with constructors, destructors, assignment operations, if only data encapsulation is possible)

In C + +, what is called class, struct, union?

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.