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

Copy constructor and assignment operator inheritance

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.