Re: explicit call to copy constructor and operator = needed

From: trying_to_learn (nospam_at_no.no)
Date: 11/18/04


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?



Relevant Pages

  • Re: operator=
    ... Compiler will supply - unless the programmer decides to do it - the ... It isn't required in an assignment: ... You will find that the code within the copy constructor and assignment ... Aside from the inlined code [i.e. the member ...
    (alt.comp.lang.learn.c-cpp)
  • Re: VS2005 - question about copy-ctor
    ... A's default constructor cannot access the default constructor for the ... derived class from breaking objects of other derived classes derived from ... Delete the member copy_protection to get around this, ... compiler to get the error in the first place. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Virtual Base Class Question
    ... >>>'A' doesn't have a default constructor. ... And since 'B' doesn't initialise ... >>>I believe the compiler complains because neither 'B' nor 'C' initialise ... compiler care *until the point* I actually came to declare a C or B????? ...
    (microsoft.public.vc.language)
  • Re: Testing if Int has a value
    ... Because in most of the languages int cannot be tested for ... If that *int* of yours is a local variable compiler is goign to ... If it is a member of a struct with constructor again the compiler is going ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: explicit call to copy constructor and operator = needed
    ... for base class and member objects in composition? ... > the default constructor is automatically called instead" ... for the "gb" member in the copy c-tor's initialiser list, ... make sure you properly initialise all the base classes and members ...
    (comp.lang.cpp)