クラスのメンバ変数を、コンストラクタのかっこの後に「:」を使って初期化する方法です。
代入と違い、オブジェクトの生成時に同時に値を設定できるため、
定数・参照・クラス型メンバ などを正しく初期化できます。
class Player { public: string name; int hp; double speed; // (例1)メンバイニシャライザを使った初期化 Player(string n, int h, double s) : name(n), hp(h), speed(s) { // コンストラクタ本体 } // (例2)通常の代入初期化 //最近割と非推奨(生成後に代入するため) Player(string n, int h, double s) { name = n; hp = h; speed = s; } };
| 項目 | 書き方 | 説明 |
|---|---|---|
| 構文 | : 変数(初期値), 変数(初期値) | クラスのメンバを生成時に同時初期化する |
| 記述位置 | コンストラクタの () の後 | { } の前に書く |
| 利点 | 代入より高速・確実 | 特に const メンバや参照メンバで必須 |
| 初期化順 | 宣言順に実行される | ソースコード内の並び順に依存しない |
🎯 目的:
🧠 考え方
🛠 コード
#include <iostream> #include <vector> #include <cmath> #include <string> #include <algorithm> #include <cstdlib> // system("cls") #include <limits> // numeric_limits using namespace std; const int BOARD_CELLS = 28; const double SCALE = 10.0; const double DT = 1.0; class Vector2D { public: double x, y; Vector2D(double ax=0, double ay=0) : x(ax), y(ay) {} Vector2D add(const Vector2D& v) const { return Vector2D(x + v.x, y + v.y); } static double distance(const Vector2D& a, const Vector2D& b) { double dx = a.x - b.x, dy = a.y - b.y; return sqrt(dx*dx + dy*dy); } }; class Player { public: string name; Vector2D pos, vel; double radius; Player(string n, Vector2D p, Vector2D v, double r) : name(n), pos(p), vel(v), radius(r) {} void update() { pos = pos.add(Vector2D(vel.x * DT, vel.y * DT)); } }; class Enemy { public: string name; Vector2D pos, vel; double radius; Enemy(string n, Vector2D p, Vector2D v, double r) : name(n), pos(p), vel(v), radius(r) {} void update() { pos = pos.add(Vector2D(vel.x * DT, vel.y * DT)); } };
🔍 確認
🎯 目的:
🧠 考え方
🛠 コード
int toCell(double worldX) { int idx = static_cast<int>(worldX / SCALE + 0.5); if (idx < 0) idx = 0; if (idx >= BOARD_CELLS) idx = BOARD_CELLS - 1; return idx; }
🔍 確認
🎯 目的:
🧠 考え方
🛠 コード
string buildSugorokuLine(int pCell, int eCell) { vector<char> cells(BOARD_CELLS, '_'); if (pCell == eCell) cells[pCell] = 'X'; else { cells[pCell] = 'P'; cells[eCell] = 'E'; } string line; for (char c : cells) { line += '|'; line += c; } line += '|'; return line; }
🔍 確認
🎯 目的:
🛠 コード
void render(const Player& p, const Enemy& e) { int pCell = toCell(p.pos.x); int eCell = toCell(e.pos.x); string line = buildSugorokuLine(pCell, eCell); cout << line << "\n"; cout << " Enemy: E(" << e.pos.x << ")\n"; cout << " Player: P(" << p.pos.x << ")\n"; }
🔍 確認
🎯 目的:
🛠 コード
int main() { Player pl("Player", Vector2D(0,0), Vector2D(3,0), 2.0); Enemy en("Enemy", Vector2D(270,0), Vector2D(-2,0), 2.0); for (int step = 0; step < 200; ++step) { system("cls"); cout << "=== Step " << step << " ===\n"; render(pl, en); double d = Vector2D::distance(pl.pos, en.pos); cout << " distance = " << d << " / threshold (rP+rE) = " << (pl.radius + en.radius) << "\n"; if (d <= (pl.radius + en.radius)) { cout << ">>> HIT! 接触しました。\n"; break; } cout << "\nEnterで次へ..."; cin.ignore(numeric_limits<streamsize>::max(), '\n'); pl.update(); en.update(); } return 0; }
🔍 確認
🎯 目的:
🛠 コード
bool hitTest(const Player& p, const Enemy& e) { double d = Vector2D::distance(p.pos, e.pos); return d <= (p.radius + e.radius); }
🔍 確認
🎯 目的:
🧠 観察のポイント
💬 メモ例
=== Step 12 === |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|P|_|_|_|_|_|_|_|E| Enemy: E(162.0) Player: P(162.0) distance = 0 / threshold (rP+rE) = 4 >>> HIT! 接触しました。