本文介绍了在没有#define的头文件中声明数组大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个以下代码(简化版):
b
$ b// public:
enum {MESSAGE_SIZE_MAX = 1024,MESSAGE_COUNT_MAX = 20};
我从不喜欢 #defines 像常量。
使用 enum 。
总是一个好习惯I have a code a following (simplified version):
#define MESSAGE_SIZE_MAX 1024 #defined MESSAGE_COUNT_MAX 20 class MyClass { public: .. some stuff private: unsigned char m_messageStorage[MESSAGE_COUNT_MAX*MESSAGE_SIZE_MAX]; };I don't like defines, which are visible to all users of MyCalss.
How can I do it in C++ style?
ThanksDima
解决方案The trick to get such things into the class definition is,
// public: enum {MESSAGE_SIZE_MAX=1024, MESSAGE_COUNT_MAX=20};I never liked #defines to be used like constants.
Its always a good practice to use enum.这篇关于在没有#define的头文件中声明数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!