[英]How to declare a new class in the header and define it in the source file without this errors?
[英]Declare array size in header file without #define's
我有以下代碼(簡化版):
#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];
};
我不喜歡MyCalss的所有用戶都可以看到的定義。
我該如何以C ++風格進行?
謝謝迪瑪
為什么不簡單地使用常量?
const int message_size_max = 1024;
請注意,與C不同,C ++默認使全局范圍內的常量變量具有靜態鏈接。
上面的常量變量是一個常量表達式,因此可以用來指定數組大小。
char message[message_size_max];
將此類內容納入class
定義的訣竅是,
// public:
enum {MESSAGE_SIZE_MAX=1024, MESSAGE_COUNT_MAX=20};
我從不喜歡將#defines
用作常量。
使用enum
始終是一個好習慣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.