--- Ven 22/5/09, mina <[email protected]> ha scritto:

> Da: mina <[email protected]>
> Oggetto: [c-prog] problem in inheritance
> A: [email protected]
> Data: Venerdì 22 maggio 2009, 08:30
>
> .. code is omitted ...
>
> which one of statements is valid?and what happend if these two statements is 
> executed?
>
> tnx!

The code is so wrong that I rewrote it so that others can copy-and-paste it and 
try to compile:

class Father // super class
{
    // defined some data so that I can see what happened
    int f_data;
};

class Child : public Father // subclass
{
    //...
    int c_data;
};

int main(){
Father f;
Child ch;

f = ch; // father = child
ch = f; // child = father
}


The code does not compile on my linux machine gcc 4.3.0. The offending line is:

ch = f // child = father

After commenting out the offending line I run the app in the debugger. 
Assigning the child to father works because all data members of 'Father' can be 
initialized with the data member of the child. The compiler can automatically 
define an operator = function.
In fact Father::f_data got the value of Child::f_data.

The contrary cannot work because there is not a operator = function defined.
So the compiler cannot automatically assign Child::c_data because Father does 
not define a 'c_data' data member.

Luciano





      

Reply via email to