C ++静态常量成员变量的用法 [英] C++ Static Const Member Variable Usage

查看:282
本文介绍了C ++静态常量成员变量的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个需要一些常量才能起作用的类.几个成员函数需要使用这些常量.由于#define可能会引起冲突,因此不予使用.常数是8或16位的十六进制模式,并存储为uint8_t或uint16_t.这些常量在类的实例之间也不会改变,因此,仅拥有一个常量副本就可以节省内存(尽管内存很少).

Say that I have a class that requires a few constants to function. Several member functions require use of these constants. Use of #define is frowned upon since it can cause collisions. The constants are hex patterns of 8 or 16 bits and are stored as uint8_t or uint16_t. These constants also don't change from instance to instance of the class, and therefore memory (albeit very little memory) can be saved by having only one copy of the constants.

是否有任何不适当的方法,或者可能是完成上述任务的更好方法,而不是简单地执行以下操作:

Is there anything improper, or perhaps of better way of accomplishing the above instead of simply doing something like the following:

// mycode.h
// .......
class myclass {
private:
  static const uint16_t kMyClassConstant_ = 0xBEEF;
// .......
};

预先感谢您的帮助.

推荐答案

鉴于您对情况的描述,我想说使用static const成员是一种好方法.在C ++ 11中,您可能希望将其更改为static constexpr,以强调它是一个编译时常量,尽管因此没有任何有效改变.

Given your description of the situation, I'd say using static const members is a good approach. In C++11 you may want to change it into static constexpr to emphasize it's a compile-time constant, although nothing will effectively change as a result of that.

如果您以一种定义规则(odr),特别是在相关的方式下引用代码中的myclass::kMyClassContant_.在需要引用(包括const-reference)的上下文中,编译器将抱怨没有常量的定义.在这种情况下,仅在类内部声明并初始化它是不够的.这可能会迫使您分开声明和定义:

If you refer to myclass::kMyClassContant_ somewhere in the code in a way that is relevant under the one-definition-rule (odr), esp. in contexts that require a reference (including const-reference), the compiler will complain that there is no definition of the constant. Merely declaring and initializing it inside the class isn't sufficient in this case. This may force you to separate declaration and definition:

// mycode.h
class myclass {
private:
  static const uint16_t kMyClassConstant_;
};

// mycode.cpp
const uint16_t myclass::kMyClassConstant_ = 0xBEEF;

为避免维护单独的声明和定义的麻烦,某些人更喜欢声明内联constexpr函数而不是实际变量:

To avoid the trouble of maintaining separate declarations and definitions, some people prefer declaring an inline constexpr function instead of an actual variable:

// mycode.h
class myclass {
private:
  static constexpr uint16_t kMyClassConstant_()
  { return 0xBEEF; }
};

这是解决许多与odr相关的问题的正确方法,并且不会造成性能损失.它是否真的有用取决于要维护一个普通静态常量的单独声明和定义要承担多少负担.如果您希望常量不会随着代码的发展而改变,那么最好使用带有单独定义的普通静态常量.但是,如果您经常修改常量的定义,则必须重新编译定义文件并将其重新链接到项目的所有相关部分,这可能会使您认为上述基于函数的解决方案是更好的选择.

This is a correct work-around for many of the odr-related problems, and it does not cause any loss in performance. Whether it is really useful depends on how much of a burden it is to maintain separate declarations and definitions of an ordinary static constant. If you expect your constants to never change as your code evolves, using ordinary static constants with separate definitions is preferable. But if you modify the definitions of the constants frequently, having to re-compile the definition file and re-link it to all relevant parts of the project may make you consider the function-based solution above as a better alternative.

关于数据类型的最后注释:如果需要以紧凑形式存储许多这些值,则使用std::uint16_t将其强制为16位可能很有用.否则,实际大小可能并不重要,在这种情况下std::uint_fast16_t(可能大于16位)可能会更好.

A final comment on the data type: Forcing it into 16 bits using std::uint16_t can be useful if you need to store lots of these values in compact form. Otherwise, the actual size may not really matter, in which case std::uint_fast16_t (which may be larger than 16 bits) may be better.

这篇关于C ++静态常量成员变量的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆