inheritance and assignment

Discussion in 'C++' started by Vasileios Zografos, Jul 1, 2003.

  1. Hi everyone,

    ok assume that I have a class named Vertex3D with 3 simple member
    variables x,y,z.
    and I create a child class named SpecialVertex3D that has an extra
    variable E.

    can I do something like

    Vertex3D v1;
    SpecialVertex3D s1;
    s1=v1;

    ???

    Well, my compiler complains so probably I cannot. any other way apart
    from creating an "operator=" method?
     
    Vasileios Zografos, Jul 1, 2003
    #1
    1. Advertising

  2. "Vasileios Zografos" <> wrote...
    > ok assume that I have a class named Vertex3D with 3 simple member
    > variables x,y,z.
    > and I create a child class named SpecialVertex3D that has an extra
    > variable E.
    >
    > can I do something like
    >
    > Vertex3D v1;
    > SpecialVertex3D s1;
    > s1=v1;
    >
    > ???


    Not unless you define how it should be done. The default operator=
    calls respective operator= functions for every member. 'v1' does
    not have member 'E'. How should the compiler assign 's1.E'?

    >
    > Well, my compiler complains so probably I cannot. any other way apart
    > from creating an "operator=" method?
    >


    No.

    Victor
     
    Victor Bazarov, Jul 1, 2003
    #2
    1. Advertising

  3. Vasileios Zografos wrote:
    > Hi everyone,
    >
    > ok assume that I have a class named Vertex3D with 3 simple member
    > variables x,y,z.
    > and I create a child class named SpecialVertex3D that has an extra
    > variable E.
    >
    > can I do something like
    >
    > Vertex3D v1;
    > SpecialVertex3D s1;
    > s1=v1;


    If you're aiming for polymorphism (as it seems from the fact that you
    specifically said you're talking about two classes in a hierarchy) it
    works only through pointers, e.g. you can assign a pointer to
    SpecialVertex3D to a pointer to Vertex3D.

    Otherwise overload an operator= if you just want to make a copy of a
    Vertex3D's member variables to the corresponding ones in
    SpecialVertex3D.. (or whatever you think fitting anyway).

    Giannis Tsiombikas
    -- Nuclear / the Lab --
     
    John Tsiombikas (Nuclear / the Lab), Jul 2, 2003
    #3
  4. "Rolf Magnus" <> wrote...
    > Victor Bazarov wrote:
    >
    > > "Vasileios Zografos" <> wrote...
    > >> ok assume that I have a class named Vertex3D with 3 simple member
    > >> variables x,y,z.
    > >> and I create a child class named SpecialVertex3D that has an extra
    > >> variable E.
    > >>
    > >> can I do something like
    > >>
    > >> Vertex3D v1;
    > >> SpecialVertex3D s1;
    > >> s1=v1;
    > >>
    > >> ???

    > >
    > > Not unless you define how it should be done. The default operator=
    > > calls respective operator= functions for every member. 'v1' does
    > > not have member 'E'. How should the compiler assign 's1.E'?
    > >
    > >>
    > >> Well, my compiler complains so probably I cannot. any other way apart
    > >> from creating an "operator=" method?
    > >>

    > >
    > > No.

    >
    > What about a conversion constructor or operator?
    >


    You're right. Those would probably work too.

    V
     
    Victor Bazarov, Jul 2, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nagy
    Replies:
    36
    Views:
    1,029
    Terry Reedy
    Jul 20, 2006
  2. Josh Mcfarlane
    Replies:
    6
    Views:
    432
    benben
    Jul 4, 2005
  3. Chris
    Replies:
    34
    Views:
    1,556
  4. Henrik Goldman
    Replies:
    2
    Views:
    446
    Andrey Tarasevich
    Dec 26, 2006
  5. Rouslan Korneychuk
    Replies:
    8
    Views:
    613
    Rouslan Korneychuk
    Feb 10, 2011
Loading...

Share This Page