Re: explicit call to copy constructor and operator = needed
From: trying_to_learn (nospam_at_no.no)
Date: 11/18/04
- Next message: Tom Widmer: "Re: expression of a function object"
- Previous message: trying_to_learn: "Re: explicit call to copy constructor and operator = needed"
- In reply to: Victor Bazarov: "Re: explicit call to copy constructor and operator = needed"
- Next in thread: Victor Bazarov: "Re: explicit call to copy constructor and operator = needed"
- Reply: Victor Bazarov: "Re: explicit call to copy constructor and operator = needed"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 18 Nov 2004 01:43:04 -0800
Victor Bazarov wrote:
> "trying_to_learn" <nospam@no.no> wrote...
>
>>Why do we need to explicitly call the copy constructor and the operator =
>>, for base class and member objects in composition?
>
>
> What?
>
>
>>...book says "You must explicitly call the GameBoard copy-constructor or
>>the default constructor is automatically called instead"
>
>
> What book is that? There is no way to "explicitly call" any c-tor.
>
> The example below concerns _initialisation_, not an "explicit call"
> to a copy constructor.
>
>
>>Why cant the compiler do this on its own. if we are making an object
>>through copr construction for an inherited class , then why not simply
>>call the corresponding copy constructors for base class objects or
>>composed objects ?
>
>
> It usually does, if you let the compiler create that copy constructor
> for you (what is known as "compiler-generated copy constructor"). But
> if you take the reins, you have to accept _all_ responsibility. You
> shouldn't expect to tell the compiler, "Wait, I'll do it", and then
> still have it mop up after you.
>
>
>>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";
>> }
>
>
> }; // was missing
>
> The point here is that if you don't specify the _initialisation_
> for the "gb" member in the copy c-tor's initialiser list, the
> compiler cannot _guess_ how you wanted to initialise that member,
> and will invoke the _default_ constructor for it.
>
> If you _need_ to implement the copy c-tor for whatever reason,
> make sure you properly initialise all the base classes and members
> (whatever "properly" means in your case).
>
> V
>
>
Thanks for ure reply Victor ...U said Quote " The point here is that if
you don't specify the _initialisation_
for the "gb" member in the copy c-tor's initialiser list, the
compiler cannot _guess_ how you wanted to initialise that member,
and will invoke the _default_ constructor for it" End Quote
My point is :The compile does not have to guess how to initialize the
member objrcts in this case. its obvious since the call to the game
class object was by value then all the member objects should also be
initialized by value and hence their copy constructors should be called
and not their default constructors?
- Next message: Tom Widmer: "Re: expression of a function object"
- Previous message: trying_to_learn: "Re: explicit call to copy constructor and operator = needed"
- In reply to: Victor Bazarov: "Re: explicit call to copy constructor and operator = needed"
- Next in thread: Victor Bazarov: "Re: explicit call to copy constructor and operator = needed"
- Reply: Victor Bazarov: "Re: explicit call to copy constructor and operator = needed"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|