作者HiSUrMy (HiSUrMy)
標題Re: [問題] c++ class 內部 array size 宣告
時間Sun Dec 9 18:16:56 2018
#ifndef A_H
#define A_H
class A
{
private:
static const int SIZE = 4;
int a[SIZE];
...
public:
...
};
#endif
or
#ifndef A_H
#define A_H
class A
{
private:
enum {SIZE = 40
};
int a[SIZE];
...
public:
...
};
#endif
※ 引述《QQrzQQ (QQ)》之銘言:
: #ifndef A_H
: #define A_H
: #define SIZE 4
: class A{
: int a[SIZE];
: };
: #endif
: 如前面所寫的
: 因為我在使用上呼叫不同標頭檔
: 會遇到SIZE重複定義的問題
: 試著使用const int在class內
: 卻無法compile過
: 想請問大家如果遇到這種問題
: 又不想用new的方式
: 有沒有其他建議的寫法
: 謝謝 :)
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.225.219.135
※ 文章網址: https://www.ptt.cc/bbs/Programming/M.1544350618.A.95F.html
推 LPH66: 1. enum 後面用 {} 不是 [] 123.195.192.32 12/10 04:00
阿
感謝版友糾錯
之前為上色使用*[m在ptt
手邊忙其他事務
沒注意到
抱歉
→ LPH66: 2. 原文推文已回答可使用 C++11 的 123.195.192.32 12/10 04:00
→ LPH66: constexpr 關鍵字來達成 123.195.192.32 12/10 04:00
→ LPH66: 3. 用 static 雖然也是能達成目的 123.195.192.32 12/10 04:00
→ LPH66: 但語意沒有 constexpr 明確 123.195.192.32 12/10 04:01
若只有限定C++11那就選constexpr吧
但原文亦可Pre-C++11
參考enum與static足矣
※ 編輯: HiSUrMy (118.169.39.154), 12/10/2018 08:42:55