473,396 Members | 1,767 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 and assignment operator inheritance

Hi,

Lets say you have class A which holds all data types as private members.
Class B then inherits from A and does *only* include a set of public
functions which uses A's existing functions for manipulation.
Now A has a copy constructor and assignment operator.
What will happen if copying goes on in B? Do I need to have an overloaded
set of functions which call the same copy functions that A has available? Or
are they virtual in the sense that B's default copying mechanisms call A's
functions in the run?

Thanks.

-- Henrik
Dec 26 '06 #1
2 6220
"Henrik Goldman" <he************@mail.tele.dkwrote in message
news:45*********************@dread11.news.tele.dk
Hi,

Lets say you have class A which holds all data types as private
members. Class B then inherits from A and does *only* include a set
of public functions which uses A's existing functions for
manipulation. Now A has a copy constructor and assignment operator.
What will happen if copying goes on in B? Do I need to have an
overloaded set of functions which call the same copy functions that A
has available? Or are they virtual in the sense that B's default
copying mechanisms call A's functions in the run?
Rules:

1A. The compiler-generated copy constructor of Derived automatically calls
the copy constructor of Base --- regardless of whether the Base copy
constructor is compiler- or user-defined.
1B. Any user-defined Derived copy constructor must explicitly call the Base
copy constructor if that is what is wanted: otherwise you get the Base
default (not copy) constructor. In other words, the user-defined copy
constructor of Derived functions behaves the same with respect to Base
constructors as does the user-defined (regular) constructor of Derived.

2A. The compiler generated assignment operator of Derived automatically
calls the assignment operator of Base --- regardless of whether the Base
assignment operator is compiler- or user-defined.
2B. Any user-defined Derived assignment operator does exactly what the user
defines it to do. It therefore must explicitly call the Base assignment
operator to make assignments to the Base subobject.
--
John Carson
Dec 26 '06 #2
Henrik Goldman wrote:
Lets say you have class A which holds all data types as private members.
Class B then inherits from A and does *only* include a set of public
functions which uses A's existing functions for manipulation.
Now A has a copy constructor and assignment operator.
What will happen if copying goes on in B? Do I need to have an overloaded
set of functions which call the same copy functions that A has available? Or
are they virtual in the sense that B's default copying mechanisms call A's
functions in the run?
If you talking about copying objects of type B, then the implicit
(compiler-provided definitions) of B's copy constructor and copy
assignment operator will automatically call corresponding constructor
and assignment operator for its base subobject of type A.

Although all this has absolutely nothing to to with anything being
"virtual"...

--
Best regards,
Andrey Tarasevich

Dec 26 '06 #3

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

Similar topics

1
by: Russ Ford | last post by:
Hi all, I'm trying to get inheritance and constructors clear in my head (and in my code). I have the following inheritance situation (all derivations public): A is the base class B is...
4
by: away | last post by:
1. When a class defined with a member of pointer type, it's necessary to have a copy constructor and assignment operator. If don't pass objects of such class as value in a function and don't do...
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
8
by: rKrishna | last post by:
I was trying to understand the real need for copy constructors. From literature, the main reason for redfinition of copy constructor in a program is to allow deep copying; meaning ability to make...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
1
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
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,...

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.