Re: Custom CRect Question
"jp2code" <poojo.com/mail> wrote in message
news:uQrL50vyHHA.4476@TK2MSFTNGP06.phx.gbl...
That's what I was needing. I had done something similar, but I was
returning a pointer (RECT*) instead of the address (RECT&).
Would this have caused different results? The whole topic of pointers
verses addresses has always been a little muddled in my brain.
Pointers *are* addresses.
If you have
int n=3;
and
int *pn = &i;
Then the pointer pn is the address where the number 3 (in this case) resides
in memory. pn and n are not interchangeable! n is the number; pn is its
address. You can get the number from the address (it's *pn) and the address
from the number (it's &n) but they are different things.
References are more subtle. They're like pointers which can be used for
many syntax purposes interchangeably with the the object itself. So if you
have
int &r = n;
r is another way of referring to n. It's a pseudo-pointer, if you like, to
n, (in that the information it indirectly encapsulates is actually the
address of n) but now you can use it for many purposes as if it were n.
Coming back to RECT abd CRect:
The CRect class is essentially a wrapper around RECT. Because of the
inheritance, its data elements (left, top, right, bottom) are exactly those
of RECT, and so CRect *is* a RECT with methods added, trnasforming it from C
to C++.
Now for some reason, which I think is a BIG mistake, when they designed
CRect they gave it two members of the form
CRect::operator LPRECT();
CRect::operator LPCRECT() const;
which cast a CRect to a pointer to a RECT. It just muddies the waters:
objects do *not* need to be cast to pointers to themselves.
Consider Windows API functions which take a pointer to a RECT. For example:
BOOL GetClientRect( HWND hWnd, RECT *prect );
In the old days of C one called it with
RECT R;
GetClientRect( hWnd, &R ); // &R is apointer to R
and one can call it with
CRect r;
GetClientRect( hWnd, &r ); // &r is apointer to r
But the existence of the cast operator means you can also write
CRect r;
GetClientRect( hWnd, r );
r is a CRect, but in fact it is cast to a pointer to itself when
GetClientRect is called.
Why on earth would anyone want to do that just to avoid typing the & - it
just looks totally confusing!!!???? Someone tell me I'm not alone here!
Dave
----
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm
CRect is effectively derived from RECT (actually from tagRECT but I guess
this is because RECT has to work in both C and C++ - don't quote me) so
you can have
RECT & CMyRect::rect()
{
return *(RECT *)(CRect *)this;
}
const RECT & CMyRect::rect() const
{
return *(const RECT *)(const CRect *)this;
}
if you really want to be pedantic :-)
[ I am always wary of using RECT at all, and prefer CRect, because its
copy constructor and assignment operator is implicit, and it offends my
sense of Cplusplusiness, though perhaps it shouldn't. I have also
never liked the RECT * conversion of CRect - it just feels horrible
converting an object to a pointer.]
Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm