Overload -> operator
-
I saw here: Flicker Free Drawing In MFC[^] in MemDC source code a line:
// Allow usage as a pointer CMemDC\* operator->() { return this; }
Sorry for this simple question: it was really necessary ? If I have an already pointer to an CMemDC object, and I write pMemDC->, then is not the same thing like this operator didn't has been overloaded ? Thank you for any clarifying !
-
I saw here: Flicker Free Drawing In MFC[^] in MemDC source code a line:
// Allow usage as a pointer CMemDC\* operator->() { return this; }
Sorry for this simple question: it was really necessary ? If I have an already pointer to an CMemDC object, and I write pMemDC->, then is not the same thing like this operator didn't has been overloaded ? Thank you for any clarifying !
It is the same if you already have a pointer. But that class is usually created as
CMemDC pDC(dc);
where you don't have a pointer. The reason for the overload is that the class is used instead of the
CDC*
which is passed to the drawing functions. So you can modify existing code for theCDC*
by just replacing the variable name without changing the access operator from->
to.
. Using such is bad practice but has been used by MFC too. It makes sense for this special class but should be generally avoided. -
It is the same if you already have a pointer. But that class is usually created as
CMemDC pDC(dc);
where you don't have a pointer. The reason for the overload is that the class is used instead of the
CDC*
which is passed to the drawing functions. So you can modify existing code for theCDC*
by just replacing the variable name without changing the access operator from->
to.
. Using such is bad practice but has been used by MFC too. It makes sense for this special class but should be generally avoided. -
I saw here: Flicker Free Drawing In MFC[^] in MemDC source code a line:
// Allow usage as a pointer CMemDC\* operator->() { return this; }
Sorry for this simple question: it was really necessary ? If I have an already pointer to an CMemDC object, and I write pMemDC->, then is not the same thing like this operator didn't has been overloaded ? Thank you for any clarifying !
I think I can help but I am struggling with the grammar and intent of your question. -> operator gives you access to the class. It is much like operator .