By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,457 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,457 IT Pros & Developers. It's quick & easy.

A simple copy ctor + inheritance qn

P: n/a
//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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"mescaline" <ap*****@columbia.edu> wrote...
//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
Jul 22 '05 #2

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<PUsHb.58992$VB2.110457@attbi_s51>...
"mescaline" <ap*****@columbia.edu> wrote...
//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


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

thanks again
m
Jul 22 '05 #3

P: n/a
"mescaline" <ap*****@columbia.edu> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message

news:<PUsHb.58992$VB2.110457@attbi_s51>...
"mescaline" <ap*****@columbia.edu> wrote...
//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


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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.