2011-08-12 21 views
0

は、コードの一部である:ここでコピーコンストラクタと代入演算子

class GameBoard 
{ 
    public: 
    GameBoard() { cout<<"Gameboard()\n"; } 
    GameBoard(const GameBoard&) 
    { 
     cout<<"GameBoard(const GameBoard&)\n"; 
    } 

    GameBoard& operator=(const GameBoard&) 
    { 
     cout<<"GameBoard& operator=(const GameBoard&)\n"; 
     return *this; 
    } 
    ~GameBoard() { cout<<"~GameBoard()\n";}; 
};  

class Game 
{ 
    GameBoard gb; 
    public: 
    Game(){ cout<<"Game()\n"; } 
    Game(const Game&g):gb(g.gb) 
    { 
     cout<<"Game(const Game&g)\n"; 
    } 
    Game(int) {cout<<"Game(int)\n"; } 
    Game& operator=(const Game& g) 
    { 
     gb=g.gb; 
     cout<<"Game::operator=()\n"; 
     return *this; 
    } 
    class Other 
    { 
     public: 
     Other(){cout<<"Game::Other()\n";} 
    }; 
    operator Other() const 
    { 
     cout<<"Game::Operator Other()\n"; 
     return Other(); 
    } 
    ~Game() {cout<<"~Game()\n";} 
}; 

class Chess: public Game {}; 

void f(Game::Other) {} 

class Checkers : public Game 
{ 
    public: 
    Checkers() {cout<<"Checkers()\n";} 
    Checkers(const Checkers& c) : Game(c) 
    { 
     cout<<"Checkers(const Checkers& c)\n"; 
    } 
    Checkers operator=(const Checkers& c) 
    { 
     Game::operator=(c); 
     cout<<"Checkers operator=(const Checkers& c)\n"; 
     return *this; 
    } 
}; 

int main() 
{ 
    Chess d1; 
    Chess d2(d1); 
    d1=d2; 
    f(d1); 
    Game::Other go; 
    Checkers c1,c2(c1); 
    c1=c2; 
} 

は、私の質問は、なぜコピーコンストラクタの最後のセットは、出力

Gameboard()      //Constructing d1 
Game() 
GameBoard(const GameBoard&) 
Game(const Game&g)     //d2(d1) 
GameBoard& operator=(const GameBoard&) 
Game::operator=()     //d1=d2 
Game::Operator Other()    //f(d1) 
Game::Other() 
Game::Other()      //go    
Gameboard()      //c1 
Game() 
Checkers() 
GameBoard(const GameBoard&)  //c2(c1) 
Game(const Game&g) 
Checkers(const Checkers& c) 
GameBoard& operator=(const GameBoard&) //c1=c2 
Game::operator=() 
Checkers operator=(const Checkers& c) 
GameBoard(const GameBoard&)    ? 
Game(const Game&g)      ? 
Checkers(const Checkers& c)    ? 
~Game() 
~GameBoard() 
~Game() 
~GameBoard() 
~Game() 
~GameBoard() 
~Game() 
~GameBoard() 
~Game() 
~GameBoard() 

と呼ばれているされていますか?

+1

すべきですか? – MGZero

+0

私は実際のコードをアップロードしました – Atishay

+0

ああ、そこにあります。スクロールバーを紛失しました – MGZero

答えて

5

Checkersの代入演算子は、通常のように参照ではなく値を返します。私たちは、コードのthatsのは、実際にこの出力を作成する見ることはでき

+0

+1、はい。それでおしまい。 –

3
Checkers operator=(const Checkers& c) 

Checkers& operator=(const Checkers& c) 
関連する問題