Copy Constructors/Assignment Operator w/ Inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Russ Ford

    Copy Constructors/Assignment Operator w/ Inheritance

    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
  • Ron Natalie

    #2
    Re: Copy Constructors/Assignment Operator w/ Inheritance


    "Russ Ford" <russford@shaw. ca> wrote in message news:fhbqrvc8l7 i92dbs72bpgf0ee oiakge9hn@4ax.c om...
    [color=blue]
    >
    > 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);
    > }[/color]

    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.
    [color=blue]
    > 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).[/color]

    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.


    Comment

    Working...