comp.lang.ada
 help / color / mirror / Atom feed
* Re: Dangerous truncation of object
@ 1992-09-21 12:59 dog.ee.lbl.gov!hellgate.utah.edu!caen!zaphod.mps.ohio-state.edu!cis.ohio-
  0 siblings, 0 replies; 2+ messages in thread
From: dog.ee.lbl.gov!hellgate.utah.edu!caen!zaphod.mps.ohio-state.edu!cis.ohio- @ 1992-09-21 12:59 UTC (permalink / raw)


In article <1992Sep21.063607.10441@lth.se> dag@control.lth.se (Dag Bruck) write
s:

>	Employee* e1 = new Employee;
>	Employee* e2 = new Supervisor;
>
>A pointer or a reference to an Employee may in fact point to a
>Supervisor object, but because MonthlyPay() is declared as "virtual"
>the correct function will be invoked.  So far so good.
>
>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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Dangerous truncation of object
@ 1992-09-21 14:35 eru.mt.luth.se!lunic!sunic!lth.se!newsuser
  0 siblings, 0 replies; 2+ messages in thread
From: eru.mt.luth.se!lunic!sunic!lth.se!newsuser @ 1992-09-21 14:35 UTC (permalink / 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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1992-09-21 14:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-09-21 14:35 Dangerous truncation of object eru.mt.luth.se!lunic!sunic!lth.se!newsuser
  -- strict thread matches above, loose matches on Subject: below --
1992-09-21 12:59 dog.ee.lbl.gov!hellgate.utah.edu!caen!zaphod.mps.ohio-state.edu!cis.ohio-

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