473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

A simple copy ctor + inheritance qn

//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
3 4442
"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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Sam Wilson [Bentley] | last post by:
If you pass a C++ object by value as an argument to a function which has a variable-length argument list (...), the MSVC7 C++ compiler does not call the object's copy constructor and will not...
7
by: skishorev | last post by:
Hi, I know What is the copy constructor. I don't know where and why we have to use copy constructor. If You know please give to me with a situation where we have to use or with a small example....
5
by: nagrik | last post by:
Hello group, Last week I picked up a thread, which pointed out that if a copy constructor is created with pointers instead of reference, there is a danger of it going in infinite recursion. ...
11
by: Kush | last post by:
Hello All, I am having problem writing some data to file and I do not understand my mistake / misconception. I am trying to write some simple text to file but the text is rather output to...
3
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and...
10
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
5
by: PLS | last post by:
I'm converting some C++ code to VC++ 2005 in native (non-managed) mode. This code doesn't use ATL, but codes the COM mechanisms directly. It has a class which is the equivalent of ATL's...
2
by: subramanian100in | last post by:
If we do not provide any ctor for a class, the compiler provides the default ctor and copy ctor if needed. Consider a class Test. Suppose we provide some ctor in class Test but do not provide...
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.