Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!csc.ti.com!ti-csl!m2.csc.ti.com!m2!perez From: perez@csc.ti.com (Edward Perez) Newsgroups: comp.lang.c++ Subject: Re: reference parameters for overloaded operators Message-ID: Date: 27 Aug 90 21:17:34 GMT Sender: usenet@csc.ti.com (USENET News System) Distribution: comp.lang.c++ Organization: TI Computer Science Center, Dallas Lines: 40 In article <56873@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes: The 'Slicing' problem arises in C++ when some "thing2" is passed by value as a parameter -- where the exact type of "thing2" is not known, because the "thing2" is actually a derivative of some base class "thing." But, in deriving thing2, more members are typically added to thing2 than are in a base class thing. Passing thing2 by value, as-if it were a "thing", causes those extra members not to be copied -- they are "sliced off." This generally leads to undesirable behavior. The solution is simple, once recognized: "never" use pass-by-value where you'd expect polymorphic behavior. Use [const] references instead. ....In the following code, PrintWith_version2 suffers the slicing problem. PrintWith_version1 avoids the problem by passing by reference -- effectively allowing different sized objects to be used as its parameter. jim, i understand what you mean by slicing and the problems one could get into. however, when i compiled the code example using AT&T 2.0 06/30/89, i got the following output: thing1_name <== thing1.Print() thing2_firstname thing2_lastname <== thing2.Print() thing1_name thing2_firstname thing2_lastname <== thing1.PW_v1(thing2); thing1_name thing2_firstname thing2_lastname <== thing1.PW_v2(thing2); according to your slicing definition, when PrintWith_version2 is called from main, a temp THING is created using thing2 since thing2 is being passed by value. therefore, shouldn't the last line not have "thing2_lastname" because THING doesnt have a name2 ?? does the fact that the functions are virtual affect this in any way ?? what do you get when you run your example through your compile ?? --------------------------------------------------------------------- edward roland perez internet: perez@csc.ti.com # computer science center csnet: perez%ti-csl@relay.cs.net # texas instruments, dallas phone: 214-995-0698(w) # ---------------------------------------------------------------------