mina wrote:
> hi.
>
> class Father // super class
> {
> ...
> }
>
> class Child : public Father // subclass
> {
> ...
> void main(){
> Father f;
> Child ch;
>
> f = ch; // statement-1
> ch = f; // statement-2
> }
> which one of statements is valid?and what happend if these two statements is
> executed?
> tnx!
Well you could try it out. The proper terminology is base class and
derived class for 'Father' and 'Child' respectively. The easiest way to
figure it out mentally is to pretend you don't know either class name
and you only understand whom you derived from. I don't do the above (so
neither should you in practice) and assuming the compiler allows it and
that the '=' operator hasn't been overloaded, 'f' would get a shallow
copy of all of 'ch's 'Father' member variables in the first statement.
In the second statement, 'ch's 'Father' member variables would get a
shallow copy of 'f's member variables.
But both statements are quite dangerous to use because it would most
likely put the target variable into a weird/invalid situation, which is
why I don't do that. To safely make derived copies, look up clone().
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleSoft.com/MyTaskFocus/