了解如何正确对待 C++ 类常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5620256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:32:52  来源:igfitidea点击:

Understanding how to correctly treat c++ class constants

c++

提问by Andry

Consider the following:

考虑以下:

namespace MyNamespace{
class MyClass {
public:
   // Public area
private:
   // Private area
protected:
   // Protected area
}; /* Class */
} /* Namespace */

And consider that I would like to define a constant which is specific for my class. I usually do the following:

并考虑到我想定义一个特定于我的班级的常量。我通常执行以下操作:

namespace MyNamespace{
// Constants
const int MYINT = 12;
const std::string MYSTR = std::string("Hello");
// Class definition
class MyClass {
public:
   // Public area
private:
   // Private area
protected:
   // Protected area
}; /* Class */
} /* Namespace */

In this way I can get my variable in this way (somewhere in my code):

通过这种方式,我可以以这种方式获取我的变量(在我的代码中的某处):

MyNamespace::MYINT;
MyNamespace::MYSTR;

Is this a good practice? Considering that constants can be treated in several ways (for example numeric constants are often treated using enum), what is the best approach to define a constant (related to a class, but that can be also useful somewhere else)?

这是一个好习惯吗?考虑到可以通过多种方式处理常量(例如,通常使用 处理数字常量enum),定义常量的最佳方法是什么(与类相关,但在其他地方也很有用)?

Thankyou

谢谢

回答by Nawaz

If you want the constants specific to the class and also want them to be useful somewhere else as you said, possibly outside the class, then define them as staticmember data in the publicsection of the class:

如果您想要特定于类的常量,并且还希望它们在您所说的其他地方有用,可能在类之外,则将它们定义为类部分中的static成员数据public

//.h file
class MyClass 
{
 public:
   //constants declarations
   static const int MYINT;
   static const std::string MYSTR;
};

//.cpp file
//constants definitions 
const int MyClass::MYINT = 12;
const std::string MyClass::MYSTR = std::string("Hello");

Usage (or access):

使用(或访问):

std::cout << MyClass::MYINT << std::endl;
std::cout << MyClass::MYSTR << std::endl;

Output:

输出:

12
Hello

Online Demo: http://www.ideone.com/2xJsy

在线演示:http: //www.ideone.com/2xJsy



You can also use enumif you want to define many integral constants and all of them are somehow related, for example this:

enum如果您想定义许多积分常数并且所有这些常数都以某种方式相关,您也可以使用,例如:

class shirt
{
 public:
   //constants declarations
   enum shirt_size
   {
        small, 
        medium,
        large,
        extra_large
   };
};

But if the integral constants are not related, then it wouldn't make much sense to define them as enum, in my opinion.

但是,如果积分常数不相关,那么enum在我看来,将它们定义为 就没有多大意义。

回答by Jon

There is no "best" solution as of course that is a very subjective term.

没有“最佳”解决方案,因为这当然是一个非常主观的术语。

Considering that you mention the constants being used somewhere else, we can say that they should be declared in either the protected(if they are to be used exclusively by derived classes) or more likely the publicsection of the class.

考虑到您提到在其他地方使用的常量,我们可以说它们应该在protected(如果它们专门由派生类使用)或更可能public在类的部分中声明。

Constants that are not of integer type should be defined as static constmembers (but you will have to be careful of the order of static initializationif there are any other static objects that refer to these constants).

非整数类型的常量应定义为static const成员(但如果有任何其他静态对象引用这些常量,则必须注意静态初始化顺序)。

Constants of integer type can either be declared as static const intor as enums, as you already mention. The discriminating factor here is whether two or more constants can be logically grouped together.

static const int正如您已经提到的,整数类型的常量既可以声明为枚举,也可以声明为枚举。这里的判别因素是两个或多个常量是否可以在逻辑上组合在一起。

For example, this is probably a good idea:

例如,这可能是一个好主意:

class MyClass {
    public:
        enum {
            Color_Red,
            Color_Green,
            Color_Blue,
        };
};

While this is not:

虽然这不是:

class MyClass {
    public:
        enum {
            Color_Red,
            Vehicle_Car,
        };
};