使用公共静态常量类成员的声明

发布于 2024-12-08 21:45:21 字数 752 浏览 0 评论 0原文

人类定义了许多公共静态常量:

class Human
{
public:

static const int NUM_FINGERS = 10;
static const int NUM_TOES = 10;
static const int NUM_HANDS = 2;
static const int NUM_FEET = 2;

//The rest of the human class here
};

不相关的类经常使用它们,并且必须用类名来限定它们:

class Unrelated
{
public:
int SomeFunction()
{
//Many uses of Human's public static constants
return Human::NUM_FINGERS + Human::NUM_TOES + Human::NUM_HANDS + Human::NUM_FEET;
}
};

在命名空间的情况下,您可以:

using namespace blah;

对于这样的情况是否有等效的?

using namespace Human; //wrong

int Unrelated::SomeFunction()
{
return NUM_FINGERS + NUM_TOES + NUM_HANDS + NUM_FEET;
}

以这种方式定义一堆常量是否被认为是糟糕的编程?

Human defines many public static constants:

class Human
{
public:

static const int NUM_FINGERS = 10;
static const int NUM_TOES = 10;
static const int NUM_HANDS = 2;
static const int NUM_FEET = 2;

//The rest of the human class here
};

An unrelated class makes use of them frequently and has to qualify them with the class name:

class Unrelated
{
public:
int SomeFunction()
{
//Many uses of Human's public static constants
return Human::NUM_FINGERS + Human::NUM_TOES + Human::NUM_HANDS + Human::NUM_FEET;
}
};

In the case of a namespace you can:

using namespace blah;

Is there an equivalent for a sitaution like this?

using namespace Human; //wrong

int Unrelated::SomeFunction()
{
return NUM_FINGERS + NUM_TOES + NUM_HANDS + NUM_FEET;
}

Is it considered bad programming to define a bunch of constants that way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

手长情犹 2024-12-15 21:45:21

不相关的类经常使用它们,并且必须使用类名来限定它们。

这有什么问题吗?这样想:ntnum_toes 哪个变量名更好?使用类 Human(如果将类更改为命名空间,则使用命名空间 Human)进一步限定名称是一件好事,而不是一件坏事。它可以帮助编译器,可以帮助编码员避免与其他名称发生意外冲突,还可以帮助其他试图理解您的代码的人。

关于名称 NUM_FINGERS 等:我建议不要使用 ALL_CAPS。有一天,有人会编写一个 NUM_FINGERS 宏,它会将您的代码变成乱码。为宏保留 ALL_CAPS 名称,然后尽量避免使用宏。

An unrelated class makes use of them frequently and has to qualify them with the class name.

What's wrong with this? Think of it this way: What is a better variable name, nt or num_toes? Further qualifying the name with the class Human (or namespace Human if you change your class to a namespace) is a good thing, not a bad thing. It helps the compiler, it helps you as a coder from accidentally colliding with some other name, and it helps another human who is trying to understand your code.

Regarding the names NUM_FINGERS, etc: I recommend not using ALL_CAPS. Someday, someone will write a NUM_FINGERS macro that will turn your code into gibberish. Reserve ALL_CAPS names for macros, and then try to avoid macros.

短暂陪伴 2024-12-15 21:45:21

假设 Unrelated 确实是 Unrelated_but_about_Humans,您可以简单地说“我与 Human 具有相同的常量”:(

class Unrelated
{
private:
    static const int NUM_FINGERS = Human::NUM_FINGERS;
    static const int NUM_TOES = Human::NUM_TOES;
    static const int NUM_HANDS = Human::NUM_HANDS;
    static const int NUM_FEET = Human::_NUM_FEET;
}

其他回复关于脆弱性等的评论仍然站着。)

Assuming that Unrelated is really Unrelated_but_about_Humans, you can simply say "I have the same constants as Human":

class Unrelated
{
private:
    static const int NUM_FINGERS = Human::NUM_FINGERS;
    static const int NUM_TOES = Human::NUM_TOES;
    static const int NUM_HANDS = Human::NUM_HANDS;
    static const int NUM_FEET = Human::_NUM_FEET;
}

(Other responses's remarks regarding fragilty, etc. still stand.)

银河中√捞星星 2024-12-15 21:45:21

这在一定程度上取决于具体情况。

这些常量与 Human 类相关,因此它们由该类“拥有”是完全合理的,并且使用命名空间名称 (Human::NUM_ARMS) 可以使它们的用法明确。 (想象一下,如果您引入新类(例如 Octopus)会发生什么。您将使用 Octopus::NUM_ARMS,这将是 8 而不是 2。感谢上帝本地化的类/命名空间名称!)

当然,在 Human/Octopus 中在这种情况下,您可能需要重新考虑使用常量,而是使用带有虚拟 GetNumArms() 方法的 Animal 类/接口,每个方法都可以重写。这将允许您的客户端代码仅与人类和章鱼松散耦合,并且适用于这两种类型的动物。这将允许基础常量对于每个类都是私有的。

或者,如果您正在以另一个方向编写代码,请考虑外部类正在计算什么。这个计算应该由谁负责?也许您应该将计算添加到 Human 类中(例如 Human::GetNumLimbs()Human::GetNumBodyParts())

最后,我们都从1970 年代,所以我建议对常量使用更具可读性的样式(例如 Human::cNumFeet 导致的眼睛出血比Human::NUM_FEET,并且不会轻易被误认为是您周围的任何宏)

It depends somewhat on the situation.

The constants relate to the Human class, so it makes perfect sense that they are "owned" by it, and using the namespaced names (Human::NUM_ARMS) makes their usage unambiguous. (Imagine what would happen if you introduced new classes, like Octopus. You'd use Octopus::NUM_ARMS, which would be 8 rather than 2. Thank goodness for the localised class/namespace names!)

Of course, in the Human/Octopus situation you might want to reconsider using constants and instead use an Animal class/interface with a virtual GetNumArms() method that each can override. This would allow your client code to be only loosely coupled with Human and Octopus, and work well for both types of Animal. This would allow the underlying constants to be private to each of these classes.

Or if you are writing code in another direction, consider what the external class is calculating. Whose responsibility should this calculation be? Perhaps you should add the calculation to the Human class (e.g. Human::GetNumLimbs(), Human::GetNumBodyParts())

Finally, we've all moved on from the 1970's, so I'd recommend using a more readable style for constants (e.g. Human::cNumFeet causes much less eye bleeding than Human::NUM_FEET, and won't be easily mistaken for any macros you have lying around)

街道布景 2024-12-15 21:45:21

不,扁平化命名空间被认为是糟糕的编程,即使用using namespace,尤其是在全局范围内。如果它是 X 类中的常量,请编写 X::constant,否则只会妨碍可读性(并且存在命名冲突的风险)。

No, it's considered bad programming to flatten the namespaces, i.e. using using namespace, especially in the global scope. If it's a constant in the X class, write X::constant, otherwise you're just hindering the readability (and risk naming collisions).

暮光沉寂 2024-12-15 21:45:21

以这种方式定义一堆常量是否被认为是糟糕的编程?

事实上,您的第二个不相关的类使用了另一个类的常量。这是一个紧密耦合的依赖关系。如果第一类由于某些原因被修改以删除/重构常量,那么您的第二类也会受到影响。避免这种依赖。

Is it considered bad programming to define a bunch of constants that way?

Indeed it is, Your second unrelated class uses the constants from another class.This is an tightly coupled dependency. if first class gets modified for some reasons to remove/refactor the constants your second class gets affected too. Avoid such dependeny.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文