Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have discovered some code that uses what I think is operator() in a different way than any I've seen. I was expecting:
C++
HDC operator()() { return dcC; }

Which works, but they had:
C++
operator HDC() { return dcC; }

What is this second form called, and where to find out more about it? It isn't in any of my books that I can see, but maybe I'm using the wrong term.

Thanks!
Posted

1 solution

In the first form HDC is the return type, in the second form is correct syntax for getting HDC via implicit type cast.

The wikipwdia brings some clarity.

please show coe how to use the first in C++ :-O
 
Share this answer
 
Comments
David O'Neil 20-Aug-14 14:09pm    
Thanks. It isn't under 'operator()' like I expected, but 'implicit' in Stroustrup revealed the location. It is filed under 11.4: conversion operators. Your link was far from being clear to me!

To use the first one: if surrounding class is 'MyDC' and the object instantiation is 'dc': just "dc()" will retrieve the dcC. Or if it is a pointer, "(*dc)()". To be clearer, "someMethodThatRequiresAplainDeviceContext((*dc)());"
KarstenK 21-Aug-14 4:11am    
I really wont write code, so dc is an object and dc() is an operator on the object.

dc() looks like a function call.
David O'Neil 21-Aug-14 12:10pm    
I would rather have an explicitly stated function call than an implicit conversion occur. I've been bitten by things I can't see, therefore I will shy away from the conversion operator now that I know about it.

Thanks for helping me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900