When "constructor initializer list" is mandatory?
1) To initialize const data member
2) To initialize reference data member
3) To call a non-default constructor in base class
4) To call a non-default constructor for a member object
For example
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;
}
};
****** Code sample taken from Thinking in C++ by Bruce Eckel **********
1 Comments:
Hi,... Great work peeps... keep writing... eagerly looking for new updates
Post a Comment
<< Home