Path: utzoo!attcan!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!rex!AS01MEF@VM.TCS.TULANE.EDU From: AS01MEF@VM.TCS.TULANE.EDU (Jeff E Mandel MD MS) Newsgroups: comp.sys.mac.programmer Subject: Re: Annoying MPW C++ warning Message-ID: <5509@rex.cs.tulane.edu> Date: 21 Dec 90 15:11:04 GMT References: <450014@hpnmdla.HP.COM> <450015@hpnmdla.HP.COM> Sender: news@rex.cs.tulane.edu Organization: Tulane University School of Medicine Lines: 48 >In comp.sys.mac.programmer, Bruce.Hoult@bbs.actrix.gen.nz writes: > > >> >Does anyone know how to eliminate this annoying warning message in >> >MPW C++? >> > >> >void foo() >> >{ >> > Rect r, r2; >> > >> > r.left = 1; >> > r.right = 2; >> > r.bottom = 3; >> > r.top = 4; >> > >> > r2 = r; >> >} >> > >> >CFront gives a "variable 'r' used before set" warning for this code. Sorry to post this to the entire list, but evidently my mail to Bruce failed to reach him. The problem has to do with assignment operations in C++. Essentially, as I understand it (which is not very well), C++ creates a temporary read-only copy of the Rect prior to making the assignment. For reasons which are not clear to me, it is completely succesful, but still needs to kvetsch about it. The solution, depending on whether you are doing an assignment or construction, is to define two methods for your class Rect; an = operator, and a constructor, each of which take readonly (e.g. const) Rect arguments. To Wit: Rect::Rect(const Rect& theRect) {[useful code goes here]} Rect& Rect::operator = (const Rect& theRect) {[useful code goes here]} This should clear up your problem. While you are at it, define a constructor which takes the four coordinates for your rect, and you can write the far more readable Rect r(1,2,3,4); Rect r2=r; Jeff E Mandel MD MS Asst Professor of Anesthesiology Tulane University School of Medicine New Orleans, LA Disclaimer: "Anyone who lets an anesthesiologist teach them C++ programming should be equally willing to let a computer science professor give them an anesthetic"