C + + Learning notes scoped to a class's constants and enumerations within a scope

Source: Internet
Author: User

One, the scope is the constant of the class

In some cases, it is useful to make the scope of a symbolic constant a class. For example, a class declaration (not a definition) might use literal 30来 to specify the length of an array, because the constant is the same for all objects, so creating a constant shared by all objects is a good idea, and perhaps we would like to do the following:

1 class Weather2 {3 private:4     const int Months = n;   Declares a constant 5     double temperature[months];6         ... 7}

Note that we are in the declaration phase, we need to declare the class when there is a character constant can be used by us, but also because we are in the declaration phase, the class declaration only describes the form of the class, do not assign any value to the data member (can imagine function declaration to understand, just tell the compiler, function name, Parameter type, a few parameters, which are to describe the form, and do not assign a value to the parameter, the class is similar), really assign the data member value is when the class is used to create an object (as if the function is actually assigned to a value in the time of the call of the functions), because only after the object is created, will be allocated in a space for you to store object data, otherwise there is no storage space, so the above declaration, although the surface gives months a value of 12, but has not created the object, there is no storage worth the space (like you told the police station that your family will be born a child, but before the child really born, Police stations do not give "children" the same as the allocation of accounts, so, months in fact, there is no value, then with a no value to declare the size of the array, of course, is not right.


There are problems, there are ways, there are two ways to achieve this goal, the same effect.

The first method is to declare an enumeration in the class. The scope of the enumeration declared in the class (not defined by OH) is the entire class, as follows:

1 class Weather2 {3 Private:4     enum {Months = n};   Declares an enumeration of 5     double temperature[months];6 ... 7}

Some readers will be puzzled, not to say, when the declaration does not assign values to data members, how can this happen? It is also important to note that declaring an enumeration in this way does not create a class data member, that is, when an object is created with this class, all objects do not contain an enumeration, months is just a symbolic name, a symbolic constant (not a variable), within the scope of the class, when the code encounters it, The compiler will replace it with 12来. Perhaps there are readers wondering if the symbolic constants do not require storage space? In fact, it is not necessary, so-called symbolic constants, is to use a string to replace the identifier that appears in the program, and the macro definition is similar to the memory is not named after the symbolic constant storage space, the above procedure is to meet months replaced by 12.

The second method is to use the keyword static:

1 class Weather2 {3 private:4     static const int Months = 12;5     double temperature[months];6 ... 7}

Not a good statement, when the object is not created, so the value is not stored? This is the characteristic of static, which declares that the variable is not dependent on the object, that is, it is not stored in the object's space, it is stored with other static variables (static), that is, the variable belongs to this class, not the specific object, but the object created by this class can of course use it, For example, Staic declaration of static variable is like your village of a hundred years old well, you use or not it, it is there, it belongs to the whole village, and not a person in the village, but, everyone in the village can draw water, it certainly does not need the police station to allocate account, haha ...

Second, the enumeration within the scope (C++11)

Note: C++11 is a new C + + standard created in 2011 and has been modified on the basis of c++98.

There are some problems with traditional enumerations, one of which is that an enumerator defined in a single enumeration can conflict, such as:

1 enum Egg {Small, Medium, Large, jumbo};2 enum T_shirt {Small, Medium, Large, xlarge};

will fail to compile because the egg small and t_shirt small are in the same scope, and they will collide. C++11 provides a new enumeration scoped to a class, such as:

1 enum class Egg {Small, Medium, Large, jumbo};2 enum class T_shirt {Small, Medium, Large, xlarge};

You can also use the keyword struct instead of class. Either way, you need to qualify the enumerator with the enumeration name:

1 egg choice = Egg::large;         The Large enumerator of the egg enum2 t_shirt Floyd = t_shirt::large;  The Large enumerator of the T_shirt enum

After the scope of the enumerator is a class, there is no conflict between the enumerators in the different enumeration definitions.

C + + Learning notes scoped to a class's constants and enumerations within a scope

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.