473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

inheritance and assignment

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?

Jul 19 '05 #1
3 1799
"Vasileios Zografos" <vz*******@bcs.org.uk> 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
Jul 19 '05 #2
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 --

Jul 19 '05 #3
"Rolf Magnus" <ra******@t-online.de> wrote...
Victor Bazarov wrote:
"Vasileios Zografos" <vz*******@bcs.org.uk> 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
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
4
by: Peter Hamilton | last post by:
I am trying to implement inheritance but I am having a difficult time with some concepts. What I am trying to do is have a Child object inherit from the Parent, and when you set a property value...
1
by: Russ Ford | last post by:
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...
4
by: Busin | last post by:
When a child class inherits from a base class, will the child class inherits everything of the base class, including all member variables and functions? Or is such inheritance "selective", like not...
5
by: Tony Johansson | last post by:
Hello! If you have the following inheritance A is the base class for the derived class B and B is the base class for the derived class C. In this case will the constructor of class C be executed...
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
6
by: mmcloughlin | last post by:
I'm learning about objects and am trying to figure out how basic inheritance works. I've got into the habit of explicitly setting the prototype object with an object literal as it seems to make the...
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
2
by: Henrik Goldman | last post by:
Hi, Lets say you have class A which holds all data types as private members. Class B then inherits from A and does *only* include a set of public functions which uses A's existing functions for...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.