C + + Primer reading notes of c++11 (iv)

Source: Internet
Author: User

1 enum with scope scoped-enumeration

I believe everyone has used the enumerator, all are non-scoped, in the header file defines the need to pay special attention to do not appear duplicate name. To solve this problem, C++11 provides an enumeration with which to use. You can use the class Enumname definition, the sample code is as follows:

enum {ZERO, one, and both}; enum class Color {RED, BLUE, GREEN};

Above is the form of an enumeration definition with no scope, the following is the form of an enumeration definition with scope, the invocation of an enumeration with scope must specify a scope, or an error will be compiled.

Another big difference is that an enumerator with scope cannot be automatically converted to an int type, and an enumerator with no scope is allowed.

2 length and forward declarations of enumerations

C++11 provides a mechanism for declaring enumeration types to take up byte lengths for enumeration types, with type qualification after the enumeration name, with the following code:

enum Long Long {ct_0, ct_1};

At the same time, C++11 provides a forward declaration mechanism for enumerations. The following code:

// enum Forward Declaration enum Long Long // unscoped, must specify a size enum class // Default int
3 definition of nested classes, inner classes

This section is not a new addition to c++11, but a more interesting and usable definition of personal feeling. Inner classes can be declared only in an external class, without the need for a direct definition. The sample code is as follows:

// Nested class class outerclass{    class  innerclass;}; class outerclass::innerclass{    };

The outer class outclass a bit like the nature of the namespace.

4 Common body support contains class members with constructors

Sometimes with union, there is a large limit, and all union members must have no constructors because the common body does not know how to initialize and reinitialize.

In C++11, the Union is allowed to define a member with a constructor, with the following code:

// union with class member Union uniontest{    int  A;    std::string  str;};

But when used, we must maintain the data validity in the union, and the compiler still does not know how to construct and deconstruct the variables in the Union. Use with caution!!

5 add tuple in standard library, support regular expression, support new random number generation mechanism

A tuple is an extension to a pair that supports any number of arbitrary types of multivariate groups. Most are used to synthesize one element of multiple data, but do not want to define a new struct or class in case. Specific tuple for the recommended reference: http://www.cplusplus.com/reference/tuple/.

In C++11, STL introduces a random number generator (Random-number engine) and a random number assignment class (Random-number distribution Class), which can be used to generate a random number for a specified range, a specified type, and a specified distribution.

Because the regular expression, I am not very familiar with, here do not introduce. Interested can read the following C + + Primer ch 17.3 or c++11 standard.

Additional Instructions

This article is mainly about C + + Primer Fifth edition of the 16th chapter to the 19th chapter on the c++11 content of the collation. New usages involving enumerations, common bodies, and nested classes.

All the code is compiled in the GCC v4.8.1 version of the test, the relevant source code can be downloaded from my git, the URL is as follows: Https://git.oschina.net/Tocy/SampleCode.git, located in the c++11 directory Cpp_ The Primer_test4.cpp file.

According to other information, C++11 also supports threading mechanisms, but is not described in C + + primer. Follow-up can refer to other information to continue to improve under.

C + + Primer reading notes of c++11 (iv)

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.