473,395 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

a question about operator overloading

Hi all,
I downloaded a code (a c++ project) over the internet and at one place I saw
the following lline:

// allow usage as a pointer.
operator CMemDC*() { return this; }

(the comment up there was written in the source)

CMemDC is the class name.

I dont understand what it does, and so I dont know what is its use and whats
going on here basically. So somebody plz explain me what does this mean and
what r the benefits ?

Regards,

-ab.
Sep 7 '06 #1
4 1226
On Thu, 7 Sep 2006 11:27:25 +0500, "Abubakar" <em**********@yahoo.com>
wrote:
>Hi all,
I downloaded a code (a c++ project) over the internet and at one place I saw
the following lline:

// allow usage as a pointer.
operator CMemDC*() { return this; }

(the comment up there was written in the source)

CMemDC is the class name.

I dont understand what it does, and so I dont know what is its use and whats
going on here basically. So somebody plz explain me what does this mean and
what r the benefits ?
That's an MFC-ism and shouldn't be emulated. To understand why someone
might have done this 15 years or so ago, consider that in many places, MFC
uses temporary objects to wrap Windows handles such as HDC, HWND, etc, and
those temporary objects are represented by pointers. Thus, it seemed
desirable to define various member functions in terms of pointers rather
than references, even though the argument is required and should have been
a reference. This allowed a function f to take, say, a CDC*, and you could
call it with f(x) instead of f(*x), assuming x was a CDC* or a pointer of a
type derived from CDC. As long as you're dealing solely with pointers, this
works OK, but to use a class like CMemDC, you have to create an object
yourself, because MFC doesn't deal in temporary CMemDC objects. Usually
CMemDC objects are local or member variables, and it makes sense to define
them as objects, rather than pointers you have to initialize with new. But
to call the aforementioned f on a CMemDC named x, you have to say f(&x),
and the MFC designers didn't like that. This led them to define the
conversion function, spelled "operator CMemDC*()", which allows you to say
f(x). This is a minor convenience at best, and as people became more
familiar with C++, it became apparent that conversion functions are the
source of many errors, so hardly anyone would write code such as this
today. I think it's better to use references for required function
parameters than pointers, and that's what I do in my MFC code when
possible; I find I can live with having to say f(*x), and sometimes I'll
even bind a local reference variable y to *x, so I can say f(y) and y.g()
instead of y->g().

--
Doug Harrison
Visual C++ MVP
Sep 7 '06 #2
Ray
Abubakar wrote:
Hi all,
I downloaded a code (a c++ project) over the internet and at one place I saw
the following lline:

// allow usage as a pointer.
operator CMemDC*() { return this; }

(the comment up there was written in the source)

CMemDC is the class name.

I dont understand what it does, and so I dont know what is its use and whats
going on here basically. So somebody plz explain me what does this mean and
what r the benefits ?
A short example will illustrate this better:

class A {
public:
int nyah;
// operator A*() { return this; }
};

Now let's say you have this method:

void nyah8(A* pa) {
pa->nyah = 8;
}

With that operator A*() above commented, the only way for you to call
nyah8 is by passing it the address of an A object explicitly, e.g.:

A a;
nyah8(&a);

However, the existence of this:

operator A*() { return this; }

Basically says "if you find a context where a pointer to A is needed,
call this method", which in this case, returns a pointer to itself.

I'd say this is a bad, bad practice though. A good place to use this
would be if your class is actually wrapping a pointer (say, CAutoPtr in
ATL). Then it's legit to have an operator T*() to return the pointer
you're wrapping, so your pointer-wrapping class can be used where raw
pointers are expected.

But using it just to shortcut typing & in front of your object when
calling functions that expect pointers is bad form, IMO.
>
Regards,

-ab.

Sep 7 '06 #3
I'd say this is a bad, bad practice though. A good place to use this would
be if your class is actually wrapping a pointer (say, CAutoPtr in ATL).
Then it's legit to have an operator T*() to return the pointer you're
wrapping, so your pointer-wrapping class can be used where raw pointers
are expected.
Having an operator T* is only half the solution for a pointer class. The
really important part is the

T& operator*()

which allows you to use -member access
Sep 7 '06 #4
On Thu, 07 Sep 2006 21:35:46 +0800, Ray <ra********@yahoo.comwrote:
>I'd say this is a bad, bad practice though. A good place to use this
would be if your class is actually wrapping a pointer (say, CAutoPtr in
ATL). Then it's legit to have an operator T*() to return the pointer
you're wrapping, so your pointer-wrapping class can be used where raw
pointers are expected.
Just be aware that blurring the identities of the wrapper and the thing
wrapped tends to create potential problems. For example, operator T* allows
the object to be used with the array syntax and converted to void*. This is
not as bad as CComPtr overloading operator& to return the address of the
raw pointer, but still, it's hard to argue against defining a member
function such as auto_ptr's "get" instead of the conversion function.

--
Doug Harrison
Visual C++ MVP
Sep 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
1
by: Tony Johansson | last post by:
Hello! I have this wrapper class Integer below that I use when testing operator overloading. A book that I read say that the expression Integer i; i+5 is translated to operator+(i,5) using the...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
36
by: utab | last post by:
Dear, I have experince in C( numerical projects, like engineering problems, scientific applications) I have the basic notion of C++ also, I have read Accelerated C++ until Chapter 7, however it...
6
by: jay | last post by:
In the c++ primer ,i get a program. A class's name is TT,and it define the operator overload! TT first; //constructor TT second(30);//constructor TT thrid(40://constructor...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.