Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand C++ class constants and class enumerations

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to understand C++ class constants and class enumerations, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

1. Quasi constant

Sometimes, we want to be able to define some constants in the class that can be used by objects of all classes.

For example, we define an array in the class and want to define a constant to initialize the length of the array. Since it is used to initialize the length of the array, this value will not change, is it feasible for us to define it as const?

Like this:

Class P {private: const int Niss15; int costs [N];...}

Unfortunately, this won't work, and the compiler will throw an error:

Say that we use the non-static variable N incorrectly, see? The description in the error message is the non-static variable, which means that the N we define here is not of type static, so it cannot be used to initialize the array.

Then how is it right? Quite simply, we can define it as a static variable.

Class P {private: static const int Niss15; int costs [N];...}

That is, the static keyword is added before const int, indicating that this is a static, that is, a static variable. This constant is stored with other static variables, not in the object, so that it is shared by all objects.

For other languages such as Java, Python, and so on, static variables in a class can be accessed directly through the class name, but not in C++.

In addition to defining static variables, another way is to define class enumerations:

Class P {private: enum {Niss15}; int costs [N];...}

It works like this, and it looks incredible.

Declaring in this way does not create class data members, for objects of the class, it does not contain enumerations, where N is just a symbolic name. When you encounter it in a class, the compiler will use 15 instead.

It is also precisely because we are only trying to create symbolic constants, not variables of enumerated types, that we do not need to provide enumerated names. This method is also used in some C++ class libraries, such as ios_base::fixed.

two。 Class enumeration

There are some problems with traditional enumerated type variables. the biggest problem is that conflicts occur when enumerators in two enumerated definitions have duplicate names:

Enum A {small, big, medium}; enum B {small, large, xlarge}

There is a small in both enumerated types, and if it is located in the same function, then a conflict will occur.

To avoid this problem, Category 11 provides a new enumeration whose scope is class and declares something like this:

Enum class A {small, big, medium}; enum class B {small, large, xlarge}

It's almost the same as the above code, except that with the addition of the keyword class, it's OK to change it to struct.

When we use it, we need to add the parsing operator:

A choice = A _ V _ V _ L _ B _ c = B::large

In addition, as we said earlier, regular enumerations are automatically converted to integers, such as when assigning values to int variables or when comparing expressions. In-scope enumerations do not implicitly convert types.

So much for the understanding of C++ class constants and class enumerations. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report