473,396 Members | 2,154 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,396 software developers and data experts.

copy constructor of parent clas

Sorry if this becomes a repost. I posted this to comp.lang.c++.moderated 1
hour ago still it does not show up there so posting this here.

Hi,
Consider class A & B both of which implement a copy constructor.
class B inherits from A.
When copy constructor of B is called, first the constructor of A gets called
My question is that, why the copy constructor for A not called.
Is there a way to have it call the copy constructor of class A?

#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A constructor called\n";};
A(A& a) { cout << "Copy A constructor called\n";};
~A() { cout << "Destructor A called\n";};
};
class B: public A
{
public:
B() { cout << "B constructor called\n";};
B(B& b) { cout << "Copy B constructor called\n"; };
~B() { cout << "Destructor B called\n";};
};
int main(void)
{
B b;
B bb = B(b);
return 0;
}

Jul 22 '05 #1
4 1591
Kench wrote:
Sorry if this becomes a repost. I posted this to comp.lang.c++.moderated 1
hour ago still it does not show up there so posting this here.
It takes moderators up to a day to get through all posts in a moderated
newsgroup, usually. You should not expect a chat room performance from
the Usenet.
Consider class A & B both of which implement a copy constructor.
class B inherits from A.
When copy constructor of B is called, first the constructor of A gets called
Not unless you specifically omit initialising the base class object.
My question is that, why the copy constructor for A not called.
You didn't initialise the base class. It calls the default c-tor.
Is there a way to have it call the copy constructor of class A?
Of course. See below.

#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A constructor called\n";};
First of all, lose all the semicolons after function bodies. It
just looks so amateurish and sloppy...
A(A& a) { cout << "Copy A constructor called\n";};
~A() { cout << "Destructor A called\n";};
};
class B: public A
{
public:
B() { cout << "B constructor called\n";};
B(B& b) { cout << "Copy B constructor called\n"; };
You need to say

B(B& b) : A(b) { ...

to make sure that the 'A' part is copy-constructed.
~B() { cout << "Destructor B called\n";};
};
int main(void)
{
B b;
B bb = B(b);
return 0;
}


Victor
Jul 22 '05 #2

Kench wrote:
Consider class A & B both of which implement a copy constructor.
class B inherits from A.
When copy constructor of B is called, first the constructor of A gets called
My question is that, why the copy constructor for A not called.
Is there a way to have it call the copy constructor of class A?

#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A constructor called\n";};
A(A& a) { cout << "Copy A constructor called\n";};
~A() { cout << "Destructor A called\n";};
};
class B: public A
{
public:
B() { cout << "B constructor called\n";};
B(B& b) { cout << "Copy B constructor called\n"; };
B(B& b) : A(b) { cout << "Copy B constructor called\n"; };
~B() { cout << "Destructor B called\n";};
};
int main(void)
{
B b;
B bb = B(b);
return 0;
}


mark

Jul 22 '05 #3

Mark A. Gibbs wrote:
int main(void)
{
B b;
B bb = B(b);
return 0;
}


Incidently, the "B bb = B(b)" is a little ugly (and tough to say :/). Go
with "B bb = b", or better yet "B bb(b)".

mark

Jul 22 '05 #4

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:h6****************@dfw-read.news.verio.net...
Kench wrote:
Sorry if this becomes a repost. I posted this to comp.lang.c++.moderated 1 hour ago still it does not show up there so posting this here.
It takes moderators up to a day to get through all posts in a moderated
newsgroup, usually. You should not expect a chat room performance from
the Usenet.
Consider class A & B both of which implement a copy constructor.
class B inherits from A.
When copy constructor of B is called, first the constructor of A gets

called
Not unless you specifically omit initialising the base class object.
My question is that, why the copy constructor for A not called.


You didn't initialise the base class. It calls the default c-tor.
Is there a way to have it call the copy constructor of class A?


Of course. See below.

#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A constructor called\n";};


First of all, lose all the semicolons after function bodies. It
just looks so amateurish and sloppy...
A(A& a) { cout << "Copy A constructor called\n";};
~A() { cout << "Destructor A called\n";};
};
class B: public A
{
public:
B() { cout << "B constructor called\n";};
B(B& b) { cout << "Copy B constructor called\n"; };


You need to say

B(B& b) : A(b) { ...

to make sure that the 'A' part is copy-constructed.
~B() { cout << "Destructor B called\n";};
};
int main(void)
{
B b;
B bb = B(b);
return 0;
}


Victor


that one worked..thanks!
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
8
by: ctick | last post by:
When defining a clas and no constructor and destructor provided, compiler generates both. What're the need for this since they do nothing as to constructing/destructing an obejct. What's...
3
by: Eric Lilja | last post by:
Hello! Consider the following complete program (with sample output). There's something wrong with my call to std::copy() in the copy constructor of the Unit class. It fails to copy the list. I...
4
by: Peter | last post by:
I want to copy a parent class instance's all datas to a child's. It's actually a C++'s copy constructor. But why the following code does not work - there is a compile error! How it should look...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
5
by: flamexx7 | last post by:
In the programme below is it possible to call copy constructor of class A, inside copy constructor of class B. #include <iostream> using namespace std; class A{ int a; public: A(int...
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.