Connecting Tech Pros Worldwide Forums | Help | Site Map

A simple copy ctor + inheritance qn

mescaline
Guest
 
Posts: n/a
#1: Jul 22 '05
//Consider the simple program with inheritance, plain init for A, copy ctr for B

#include <iostream>
using namespace std;

class Base{
public:
Base(){cout << "Base, default" << endl;}
Base(const Base &){cout << "Base, Copy Ctor" << endl;}
};


class Derived:public Base{
public:
Derived(){cout << "Derived, default" << endl;}
Derived(const Derived &){cout << "Derived, Copy Ctor" << endl;}
}

int main(){

Derived A;
cout << endl;
Derived B(A);
return 0;
}

OUTPUT:

Base, default } for A, Base part
Derived, default } for A, Derived part

Base, default ->Why default when its the base class init for copy-constructed B?
Derived, Copy Ctor } for B, Derived part

What's my mistake so that I'm getting "Base, default" instead of
"Base, Copy Ctor" in the 3 rd line of the Output?

thanks
m

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

re: A simple copy ctor + inheritance qn


"mescaline" <apt2003@columbia.edu> wrote...[color=blue]
> //Consider the simple program with inheritance, plain init for A, copy ctr[/color]
for B[color=blue]
>
> #include <iostream>
> using namespace std;
>
> class Base{
> public:
> Base(){cout << "Base, default" << endl;}
> Base(const Base &){cout << "Base, Copy Ctor" << endl;}
> };
>
>
> class Derived:public Base{
> public:
> Derived(){cout << "Derived, default" << endl;}
> Derived(const Derived &){cout << "Derived, Copy Ctor" << endl;}
> }[/color]

Semicolon missing here. Are you sure you didn't just type this
in instead of copying and pasting?
[color=blue]
>
> int main(){
>
> Derived A;
> cout << endl;
> Derived B(A);
> return 0;
> }
>
> OUTPUT:
>
> Base, default } for A, Base part
> Derived, default } for A, Derived part
>
> Base, default ->Why default when its the base class init for[/color]
copy-constructed B?[color=blue]
> Derived, Copy Ctor } for B, Derived part
>
> What's my mistake so that I'm getting "Base, default" instead of
> "Base, Copy Ctor" in the 3 rd line of the Output?[/color]

You do nothing to initialise the Base part of Derived using the Base's
copy c-tor. The Derived copy c-tor ought to look like this:

Derived(const Derived &d) : Base(d)
{ cout << "Derived, Copy Ctor" << endl; }

to produce your desired ouptut, otherwise the Base part is simply default-
initialised (which is what you saw).

Victor


mescaline
Guest
 
Posts: n/a
#3: Jul 22 '05

re: A simple copy ctor + inheritance qn


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:<PUsHb.58992$VB2.110457@attbi_s51>...[color=blue]
> "mescaline" <apt2003@columbia.edu> wrote...[color=green]
> > //Consider the simple program with inheritance, plain init for A, copy ctr[/color]
> for B[color=green]
> >
> > #include <iostream>
> > using namespace std;
> >
> > class Base{
> > public:
> > Base(){cout << "Base, default" << endl;}
> > Base(const Base &){cout << "Base, Copy Ctor" << endl;}
> > };
> >
> >
> > class Derived:public Base{
> > public:
> > Derived(){cout << "Derived, default" << endl;}
> > Derived(const Derived &){cout << "Derived, Copy Ctor" << endl;}
> > }[/color]
>
> Semicolon missing here. Are you sure you didn't just type this
> in instead of copying and pasting?
>[color=green]
> >
> > int main(){
> >
> > Derived A;
> > cout << endl;
> > Derived B(A);
> > return 0;
> > }
> >
> > OUTPUT:
> >
> > Base, default } for A, Base part
> > Derived, default } for A, Derived part
> >
> > Base, default ->Why default when its the base class init for[/color]
> copy-constructed B?[color=green]
> > Derived, Copy Ctor } for B, Derived part
> >
> > What's my mistake so that I'm getting "Base, default" instead of
> > "Base, Copy Ctor" in the 3 rd line of the Output?[/color]
>
> You do nothing to initialise the Base part of Derived using the Base's
> copy c-tor. The Derived copy c-tor ought to look like this:
>
> Derived(const Derived &d) : Base(d)
> { cout << "Derived, Copy Ctor" << endl; }
>
> to produce your desired ouptut, otherwise the Base part is simply default-
> initialised (which is what you saw).
>
> Victor[/color]

WoW, does indeed work--
Can you please explain WHY my code wouldn't be giving the same answer as this?

thanks again
m
Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 22 '05

re: A simple copy ctor + inheritance qn


"mescaline" <apt2003@columbia.edu> wrote...[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message[/color]
news:<PUsHb.58992$VB2.110457@attbi_s51>...[color=blue][color=green]
> > "mescaline" <apt2003@columbia.edu> wrote...[color=darkred]
> > > //Consider the simple program with inheritance, plain init for A, copy[/color][/color][/color]
ctr[color=blue][color=green]
> > for B[color=darkred]
> > >
> > > #include <iostream>
> > > using namespace std;
> > >
> > > class Base{
> > > public:
> > > Base(){cout << "Base, default" << endl;}
> > > Base(const Base &){cout << "Base, Copy Ctor" << endl;}
> > > };
> > >
> > >
> > > class Derived:public Base{
> > > public:
> > > Derived(){cout << "Derived, default" << endl;}
> > > Derived(const Derived &){cout << "Derived, Copy Ctor" << endl;}
> > > }[/color]
> >
> > Semicolon missing here. Are you sure you didn't just type this
> > in instead of copying and pasting?
> >[color=darkred]
> > >
> > > int main(){
> > >
> > > Derived A;
> > > cout << endl;
> > > Derived B(A);
> > > return 0;
> > > }
> > >
> > > OUTPUT:
> > >
> > > Base, default } for A, Base part
> > > Derived, default } for A, Derived part
> > >
> > > Base, default ->Why default when its the base class init for[/color]
> > copy-constructed B?[color=darkred]
> > > Derived, Copy Ctor } for B, Derived part
> > >
> > > What's my mistake so that I'm getting "Base, default" instead of
> > > "Base, Copy Ctor" in the 3 rd line of the Output?[/color]
> >
> > You do nothing to initialise the Base part of Derived using the Base's
> > copy c-tor. The Derived copy c-tor ought to look like this:
> >
> > Derived(const Derived &d) : Base(d)
> > { cout << "Derived, Copy Ctor" << endl; }
> >
> > to produce your desired ouptut, otherwise the Base part is simply[/color][/color]
default-[color=blue][color=green]
> > initialised (which is what you saw).
> >
> > Victor[/color]
>
> WoW, does indeed work--
> Can you please explain WHY my code wouldn't be giving the same answer as[/color]
this?

You didn't use the member initialisation list. If you don't specify
how you want your members (and base classes are kind of members)
initialised, the compiler creates the code that default-initialises
the members that are not mentioned. For classes with non-trivial
default constructor it means invoking the default constructor. That
is what you got.

Example:

#include <iostream>

struct BaseOne {
BaseOne() { std::cout << "BaseOne Default\n"; }
BaseOne(BaseOne const&) { std::cout << "BaseOne Copy\n"; }
};

struct BaseTwo {
BaseTwo() { std::cout << "BaseTwo Default\n"; }
BaseTwo(BaseTwo const&) { std::cout << "BaseTwo Copy\n"; }
};

struct Derived : BaseOne, BaseTwo {
Derived(Derived const& d) : BaseTwo(d) {}
};

In the example above the 'BaseOne' part of 'Derived' will be
default-initialised, whereas the 'BaseTwo' part will be initialised
as you tell the compiler, using the copy.

Another example:

struct SomeClass {
int a;
SomeClass() : a(42) {}
SomeClass(SomeClass const&) {} // note: no member initialisation
list
}

int main() {
SomeClass sc;
SomeClass scc(sc);

// What do you expect scc.a to be? Why?
// What needs to be changed so that the copy is made "correctly"?

return 0;
}

Victor


Closed Thread