使用公共静态常量类成员的声明
人类定义了许多公共静态常量:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这有什么问题吗?这样想:
nt
或num_toes
哪个变量名更好?使用类Human
(如果将类更改为命名空间,则使用命名空间Human
)进一步限定名称是一件好事,而不是一件坏事。它可以帮助编译器,可以帮助编码员避免与其他名称发生意外冲突,还可以帮助其他试图理解您的代码的人。关于名称
NUM_FINGERS
等:我建议不要使用 ALL_CAPS。有一天,有人会编写一个NUM_FINGERS
宏,它会将您的代码变成乱码。为宏保留 ALL_CAPS 名称,然后尽量避免使用宏。What's wrong with this? Think of it this way: What is a better variable name,
nt
ornum_toes
? Further qualifying the name with the classHuman
(or namespaceHuman
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 aNUM_FINGERS
macro that will turn your code into gibberish. Reserve ALL_CAPS names for macros, and then try to avoid macros.假设
Unrelated
确实是Unrelated_but_about_Humans
,您可以简单地说“我与Human
具有相同的常量”:(其他回复关于脆弱性等的评论仍然站着。)
Assuming that
Unrelated
is reallyUnrelated_but_about_Humans
, you can simply say "I have the same constants asHuman
":(Other responses's remarks regarding fragilty, etc. still stand.)
这在一定程度上取决于具体情况。
这些常量与 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 thanHuman::NUM_FEET
, and won't be easily mistaken for any macros you have lying around)不,扁平化命名空间被认为是糟糕的编程,即使用
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 theX
class, writeX::constant
, otherwise you're just hindering the readability (and risk naming collisions).事实上,您的第二个不相关的类使用了另一个类的常量。这是一个紧密耦合的依赖关系。如果第一类由于某些原因被修改以删除/重构常量,那么您的第二类也会受到影响。避免这种依赖。
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.