Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > inheritance and assignment

Reply
Thread Tools

inheritance and assignment

 
 
Vasileios Zografos
Guest
Posts: n/a
 
      07-01-2003
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?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-01-2003
"Vasileios Zografos" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
John Tsiombikas (Nuclear / the Lab)
Guest
Posts: n/a
 
      07-02-2003
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 --

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-02-2003
"Rolf Magnus" <(E-Mail Removed)> wrote...
> Victor Bazarov wrote:
>
> > "Vasileios Zografos" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
Copy constructor and assignment operator inheritance Henrik Goldman C++ 2 12-26-2006 05:07 PM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
Question about inheritance and pointer assignment Josh Mcfarlane C++ 6 07-04-2005 12:47 AM



Advertisments