comp.lang.ada
 help / color / mirror / Atom feed
From: eru.mt.luth.se!lunic!sunic!lth.se!newsuser@bloom-beacon.mit.edu  (Dag Bru
Subject: Re: Dangerous truncation of object
Date: 21 Sep 92 14:35:53 GMT	[thread overview]
Message-ID: <1992Sep21.143553.18736@lth.se> (raw)

In <comp.lang.ada> firth@sei.cmu.edu (Robert Firth) writes:
>In article <1992Sep21.063607.10441@lth.se> dag@control.lth.se (Dag Bruck) writ
es:
>
>>	Employee* e1 = new Employee;
>>	Employee* e2 = new Supervisor;
>>
>>The problem arises (most commonly) when we pass objects by value to
>>functions.
>>
>>	void MakeCheck(Employee* e);	// ok
>>	void MakeCheck(Employee e);	// dangerous
>>
>>If we pass a pointer or a reference as a function argument, the
>>polymorphic behaviour is preserved.  If we pass the object by value,
>>the object is truncated to its Employee part.
>
>Maybe I'm about to display a ludicrous ignorance of object-oriented
>programming, but I don't see the problem here.  If an "employee" can
>be either of two objects, then all operations, in principle, are
>polymorphic.  So, if the compiler has to select the correct "monthlypay"
>operation, it also has to select the correct "passbyvalue" operation;
>by the same reasoning and, presumably, with a similar implementation.

It is not an "employee" that is either of two things; a pointer or a
reference can either point to an "employee" or to any class derived
from "employee".  That is quite well defined, and polymorphism is
maintained.  The dangerous case is when we ask the compiler to
truncate the object itself.

Maybe I should point out that the behaviour is clear from the
definition of C++, but it is a pitfall because a small change in the
source code changes a program from pass-by-reference to pass-by-value.
It would be much safer if the designer of "supervisor" could specify
that truncation is illegal.


		-- Dag

             reply	other threads:[~1992-09-21 14:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1992-09-21 14:35 eru.mt.luth.se!lunic!sunic!lth.se!newsuser [this message]
  -- strict thread matches above, loose matches on Subject: below --
1992-09-21 12:59 Dangerous truncation of object dog.ee.lbl.gov!hellgate.utah.edu!caen!zaphod.mps.ohio-state.edu!cis.ohio-
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox