Created
February 28, 2017 06:47
-
-
Save yonta/04b87cb112c594a17d903fd8d2ad908a to your computer and use it in GitHub Desktop.
C++むつかしい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
1. クラス内で配列データを持ちたい | |
2. 配列は定数長 | |
3. 定数長値をメソッドで使いたい | |
4. .hは定義、.cppは本体に分けたい | |
*/ | |
// .h | |
#define N 4 // 最終手段感 | |
class Example1 { | |
double data[N]; | |
} | |
// .cpp | |
// empty | |
/*******************************************/ | |
// .h | |
class Example2 { | |
static const int N = 4; // 無理やり変数化 | |
double data[N]; // ここで呼び出すので、cpp側に書けない | |
} | |
// .cpp | |
// empty | |
/*******************************************/ | |
// .h | |
class Example3 { | |
public: | |
Example3(); | |
private: | |
static const int N; // 宣言のみ | |
std::vector<double> data; | |
} | |
// .cpp | |
const int Example3::N = 4; // 実体をcppへキレイにかける | |
Example3::Example3() : data(N) {} | |
/*******************************************/ | |
// .h | |
class Example4 { | |
public: | |
Example4(); | |
private: | |
static const int N; | |
std::array<double, N> data; // ここがエラーになるのでダメ | |
} | |
// .cpp | |
const int Example4::N = 4; | |
Example4::Example4() : data() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment