473,626 Members | 3,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy constructor of parent clas

Sorry if this becomes a repost. I posted this to comp.lang.c++.m oderated 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 1617
Kench wrote:
Sorry if this becomes a repost. I posted this to comp.lang.c++.m oderated 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.********@com Acast.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++.m oderated 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
13208
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
5756
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
8
3260
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 happening in constructor/destructor if they both are defaulted and empty? Thanks!
3
1561
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 could solve it by looping manually through the argument's list and call push_back for each element, but I'd like to know what's wrong with my std::copy() call. Here's the code with sample output: #include <algorithm> /* std::copy() */ #include...
4
17182
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 like? (The background is I don't know (I don't care indeed) all members in DataGrid, so I don't want to copy all members in DataGrid one by one.) public class GridEx : DataGrid { public GridEx()
45
6337
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
5
2310
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 temp=0):a(temp){} A(A& right):a(right.a){} };
4
4159
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? class Parent { Parent::Parent() { child = new Child( this) }; Child *child; };
13
2459
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 certain code syntax but more a 'code architecture' thing , I'll use simple example classes (which are certainly not complete or working...) just to illustrate the idea (and I may make some mistakes because I'm not that experienced...). The...
0
8262
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6122
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1507
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.