0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

c++ > クラス内でstatic const [ ]宣言できない

Last updated at Posted at 2015-08-29
動作確認
C++ Builder XE4
Rad Studio 10.2 Tokyo Update 2 (追記: 2017/12/26)

String型の配列をconstで持とうとしたら以下のエラーがでた。

class myClass {
private:
   static const String errorLevels[4] = {
     L"High", L"Middle", L"Low", L"Debug"
   };
};
E2233 クラスメンバ変数を初期化できない

どうも以下と関係あるようだ
http://www.cplusplus.com/forum/beginner/44486/

You can only define static const integral values (i.e. short, int, long, bool, char, instance of an enum) within a struct. You can't declare static const arrays of integral values within a struct, unfortunately.
...
Since your array is so short, you might consider doing this:

struct A{
   static const int m1 = 1;
   static const int m2 = 3;
   static const int m3 = 5;
};

配列でないのが拡張性に問題あり。

.cppで持たせることにした。
.hにconstを持つのは、API使用者に開示したい場合かもしれない。今回の場合は、別に開示したい情報でもない。

関連

cpp > クラス内での静的定数の宣言 > class MyAPI { public: static const int MAX_NAME_LENGTH; ...

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?