inheritance and assignment

  • Thread starter Vasileios Zografos
  • Start date
V

Vasileios Zografos

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?
 
V

Victor Bazarov

Vasileios Zografos said:
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
 
J

John Tsiombikas (Nuclear / the Lab)

Vasileios said:
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 --
 

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,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top