A simple copy ctor + inheritance qn

M

mescaline

//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
 
V

Victor Bazarov

mescaline said:
//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;}
}

Semicolon missing here. Are you sure you didn't just type this
in instead of copying and pasting?
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?

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
 
M

mescaline

Victor Bazarov said:
Semicolon missing here. Are you sure you didn't just type this
in instead of copying and pasting?


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

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

thanks again
m
 
V

Victor Bazarov

mescaline said:
"Victor Bazarov" <[email protected]> wrote in message

WoW, does indeed work--
Can you please explain WHY my code wouldn't be giving the same answer as
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,208
Latest member
RandallLay

Latest Threads

Top