comp.lang.ada
 help / color / mirror / Atom feed
From: dog.ee.lbl.gov!hellgate.utah.edu!caen!zaphod.mps.ohio-state.edu!cis.ohio- state.edu!news.sei.cmu.edu!firth@ucbvax.Berkeley.EDU  (Robert Firth)
Subject: Re: Dangerous truncation of object
Date: 21 Sep 92 12:59:36 GMT	[thread overview]
Message-ID: <1992Sep21.125936.25779@sei.cmu.edu> (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.

             reply	other threads:[~1992-09-21 12:59 UTC|newest]

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

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