Copy Constructors/Assignment Operator w/ Inheritance

R

Russ Ford

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 inherited from A
C is inherited from B

D is inherited from A
E is inherited from D.

Simple enough. Now here's the deal:

A's data is all basic data types (all doubles, to be precise).
B includes a CArray (which doesn't have a = that I understand).
C includes some more numbers.

D and E only include additional numbers.

The code I'm writing is complaining that there's no = operator defined
for a class D or E assignment. In essence, "memberwise copying" will
be fine for data in all classes except B, where I need to write some
code.

I want to make all assignments work as simply as possible.

Essentially, the role of the = in most cases is a combination of the
destructor and copy constructor, right? Is there an easy way to do
this (eg:)
A& A::eek:perator = (A& a)
{
~A::A();
A::A(a);
}

I'd like to avoid defining = and copy constructors for all classes,
simply because in the case of D and E they'll call the base class'
function and then just do assignment to the passed class' members (and
it seems like the default behaviour should do that).

My solution for copy constructors was to provide one for B and A, and
have B call A. Then I thought that C, D, and E would just initialize
their members "memberwise", and call the base class copy constructor.
Is this right? How do I get assignments (=) to work then?

Sorry if I didn't explain very well. Thanks in advance for your help.

Russ
 
R

Ron Natalie

Russ Ford said:
Essentially, the role of the = in most cases is a combination of the
destructor and copy constructor, right? Is there an easy way to do
this (eg:)
A& A::eek:perator = (A& a)
{
~A::A();
A::A(a);
}

Ugh. First, assignment is not necessarily destruction followed by construction.
Second, you CANT CALL CONSTRUCTORS! You could try placement new
over the top of "this" but I'll puke on you if you do.
I'd like to avoid defining = and copy constructors for all classes,
simply because in the case of D and E they'll call the base class'
function and then just do assignment to the passed class' members (and
it seems like the default behaviour should do that).

This is why you should endeavor to have each class bheave properly.
If you define copy constructors and copy assignment operators for
the class that has the stupid CArray on it, there's no need to molest
any of your other classes. Frankly, if you'd avoid using microsoft's
piece of shit containers and use something well thought out like std::vector
you'd not have to write anything at all.
 

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

Forum statistics

Threads
473,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top