Detailed description of aggregate type in C ++

Source: Internet
Author: User

I have seen this statement in some formal descriptions of C. For example, in C standards, I thought it was just a creation concept. In Generic Programming and STL, I saw a precise definition of this concept, and the association of specific language features with this concept:
Class Test {
Public:
Int ia [3];
Int x;
} T = {1, 2,}, 4 };

The above class definition can be initialized using a structure similar to the C language. Not every class can be like this, but it is exactly aggregate type. It requires several conditions:

1. There cannot be any constructor explicitly declared by the user (because C always creates a default constructor for the class in terms of concept, so it emphasizes that the user explicitly declares the constructor)

2. you cannot have any private or protected data members, but you can have such a member function. This does not mean that the two keywords cannot appear before the data members, for example, the following redundant private does not affect the type of aggregate type:
Class T {
Private: public: // private: but it does not work.
Int x;
};

3. you cannot own virtual member functions (non-virtual member functions are allowed) or base class. this article is not mentioned in this book. After all, programmers cannot specify how vtbl should be declared.
VC2008 provides the following error message for the quasi-aggregate type of the specified base class:
Types with a base are not aggregate
The error message for a declared virtual function (although undefined) is:
Types with virtual member functions or virtual bases are not aggregate

It is easy to wonder that aggregate type allows the class to have the destructor explicitly declared by the definer, or explicitly call the dtor.

It seems that this aggregate type does not have much to do with the POD type.

In the initialization syntax of aggregate type, it should be conceptually specified with nested {}. If all elements are explicitly specified, you can remove nested {}, as shown in

Class Test {
Public:
Int ia [3];
Int x;
} T = {1, 2, 3}, 4 };
Or
T = {1, 2, 3, 4 };
You must use nested {} only when the nested {} contains the remaining uninitialized elements {}:
T = {1, 2,}, 4 };

Note that in the initialization syntax, you can always have an additional comma in the last element, even if no more elements can be specified after the time (t = {1, 2, 3 ,}, 4 ,};)

Specifying an empty element in the nested {} is legal (but a comma is not allowed)
T = {{}, 4}; // valid

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.