469,626 Members | 883 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

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 5986
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by away | last post: by
10 posts views Thread by utab | last post: by
8 posts views Thread by rKrishna | last post: by
13 posts views Thread by blangela | last post: by
1 post views Thread by blangela | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.