私はC++で思考を読んでいます。わからないコードの断片があります。誰かが私がそれを説明するのを手伝ってもらえますか?
class GameBoard {
public:
GameBoard() { cout << "GameBoard()\n"; }
GameBoard(const GameBoard&) {
cout << "GameBoard(const GameBoard&)\n";
}
GameBoard& operator=(const GameBoard&) {
cout << "GameBoard::operator=()\n";
return *this;
}
~GameBoard() { cout << "~GameBoard()\n"; }
};
class Game {
GameBoard gb; // Composition
public:
// Default GameBoard constructor called:
Game() { cout << "Game()\n"; }
// You must explicitly call the GameBoard
// copy-constructor or the default constructor
// is automatically called instead:
Game(const Game& g) : gb(g.gb) {
cout << "Game(const Game&)\n";
}
Game(int) { cout << "Game(int)\n"; }
Game& operator=(const Game& g) {
// You must explicitly call the GameBoard
// assignment operator or no assignment at
// all happens for gb!
gb = g.gb;
cout << "Game::operator=()\n";
return *this;
}
class Other {}; // Nested class
// Automatic type conversion:
operator Other() const {
cout << "Game::operator Other()\n";
return Other();
}
~Game() { cout << "~Game()\n"; }
};
上記のコードフラグメントでは、私は理解していません:
operator Other() const {
cout << "Game::operator Other()\n";
return Other();
}
この関数は演算子""を定義していると思いますOther()
。戻るとはどういう意味Other()
ですか?Other()
がクラスOtherのオブジェクトを意味する場合、演算子" Other()
"の戻り型はクラスの型ではありませんOther
。