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

Copy Constructors/Assignment Operator w/ Inheritance

P: n/a
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::operator = (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
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Russ Ford" <ru******@shaw.ca> wrote in message news:fh********************************@4ax.com...

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::operator = (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.
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.