Copy constructor and assignment operator inheritance

H

Henrik Goldman

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
 
J

John Carson

Henrik Goldman said:
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.
 
A

Andrey Tarasevich

Henrik said:
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"...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top