[comp.sys.mac.programmer] Annoying MPW C++ warning

dan@hpnmdla.HP.COM (Dan Pleasant) (12/18/90)

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.
I'd like to get rid of the warning message for no other reason than
the fact that it's annoying.  Does anybody know how to eliminate this
warning (without turning off all other warnings at the same time)?

[Note: Nitpickers will probably point out that the warning goes away
if you use SetRect() here instead of setting each rect element one at a
time.  But that doesn't solve the problem in a general case -- the
structure in this example doesn't have to be a Rect.]

Thanks in advance to one and all!

Dan Pleasant

Bruce.Hoult@bbs.actrix.gen.nz (12/19/90)

Dan Pleasant 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.


Mine doesn't (MPW CFront 1.0).  Once I #include <QuickDraw.h>, of course.
-- 
Bruce.Hoult@bbs.actrix.gen.nz   Twisted pair: +64 4 772 116
BIX: brucehoult                 Last Resort:  PO Box 4145 Wellington, NZ

dan@hpnmdla.HP.COM (Dan Pleasant) (12/21/90)

In comp.sys.mac.programmer, Bruce.Hoult@bbs.actrix.gen.nz writes:


>   Dan Pleasant 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.
>
>
>    Mine doesn't (MPW CFront 1.0).  Once I #include <QuickDraw.h>, of course.
>    -- 
>    Bruce.Hoult@bbs.actrix.gen.nz   Twisted pair: +64 4 772 116
>    BIX: brucehoult                 Last Resort:  PO Box 4145 Wellington, NZ

Well, let this be a lesson to us all.  This problem popped up twice in
my "real" code, and I thought I had it completely pinned down so I
just posted the above code snippet.  The snippet, of course, compiles
perfectly.  My original code (which is obviously a great deal more
complicated than the snippet) still exhibits the problem.  Looks like
something more complicated is going on and it's back to the drawing
board.  *Sigh*

The moral: Don't post code snippets unless you test them first!

Dan Pleasant

AS01MEF@VM.TCS.TULANE.EDU (Jeff E Mandel MD MS) (12/21/90)

>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"