473,396 Members | 1,773 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,396 software developers and data experts.

Pitfalls of user-defined conversion operator (to a primitive type)?

Hi all,

I've got a very simple "container" class that encapsulates an (presumably
opaque) OS handle, which is typedef'd in the OS headers to be equivalent
to an unsigned int (I'm simplifying a bit, but the gist of it is that
this handle is essentially a basic/primitive integer type).
typedef unsigned int MYHANDLE;

class my_auto_handle
{
public:
my_auto_handle(MYHANDLE h)
{
m_handle = h;
}

~my_auto_handle()
{
destroyhandle(m_handle);
}

operator const MYHANDLE()
{
return m_handle;
}

bool operator ==(MYHANDLE other)
{
return (m_handle == other);
}

private:
my_auto_handle(const my_auto_handle& other);
my_auto_handle& operator=(const my_auto_handle& other);

MYHANDLE m_handle;
};
(the private copy constructor and assignment operator are because the
handle musn't be destroyed twice, so I want to prevent user code from
making copies of my_auto_handle objects)
ANYWAY, my question is thus:

Given that I need to use this my_auto_handle object in code where I'll be
comparing instances of it to 'raw' MYHANDLEs, comparing it to
literals/immediates (#define INVALID_MYHANDLE (MYHANDLE)0) and passing it
to functions that take a const MYHANDLE argument:

1) Are there any situations where the auto-magic conversion operator
falls short?

2) Do I actually need to overload the == operator if I want to compare a
my_auto_handle to a MYHANDLE, which is already a basic type (I'm afraid
of accidentally comparing a MYHANDLE to, say, the address of a
my_auto_handle object instead of the handle value contained within)?
Looking forward to any and all replies!
Best Regards
JL
Nov 2 '08 #1
1 2519


John Lampe wrote:
Hi all,

I've got a very simple "container" class that encapsulates an (presumably
opaque) OS handle, which is typedef'd in the OS headers to be equivalent
to an unsigned int (I'm simplifying a bit, but the gist of it is that
this handle is essentially a basic/primitive integer type).
typedef unsigned int MYHANDLE;

class my_auto_handle
{
public:
my_auto_handle(MYHANDLE h)
{
m_handle = h;
}

~my_auto_handle()
{
destroyhandle(m_handle);
}

operator const MYHANDLE()
{
return m_handle;
}

bool operator ==(MYHANDLE other)
{
return (m_handle == other);
}

private:
my_auto_handle(const my_auto_handle& other);
my_auto_handle& operator=(const my_auto_handle& other);

MYHANDLE m_handle;
};

>
2) Do I actually need to overload the == operator if I want to compare a
my_auto_handle to a MYHANDLE, which is already a basic type (I'm afraid
of accidentally comparing a MYHANDLE to, say, the address of a
my_auto_handle object instead of the handle value contained within)?
In your case you don't have to overload the == operator, because you
have implicit conversion to MYHANDLE in your constructor. Moreover, if
you try something like
my_auto_handle m(10);
if ( m == 20 ) {//do something}
you end up with compilation error, because operator== has two
overloads now. So you can either remove the overloaded ==, or make the
constructor explicit
explicit my_auto_handle(MYHANDLE h)
{
m_handle = h;
}

Regards
Nov 3 '08 #2

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

Similar topics

8
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
14
by: ES Kim | last post by:
Consider: #include <string> #include <iostream> using namespace std; struct S { const char* ps_; operator string();
5
by: Philip Pemberton | last post by:
Hi, I've just been trying to get some (pretty badly written) code working on a different compiler. Unfortunately, I've hit a problem. When the code below is compiled under Borland C++ it...
10
by: Whitney Kew | last post by:
Hello, I have a question regarding type conversion operators. Let's say I have a simple (nonsensical) class as follows: class MakeNewInt { private: int* _n;
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
14
by: Matt | last post by:
I want to know if "int" is a primitive type, or an object? For example, the following two approaches yield the same result. > int t1 = int.Parse(TextBox2.Text); //method 1 > int t2 =...
11
by: Demorsy | last post by:
I want to convert a string (or object) to a primitive type. But I don't know the type to convert to at run time. For example l have variable (lets say its an int): int unknownType = 0; And a...
1
by: v4vijayakumar | last post by:
Can "c++ conversion operator" be considered as an example for Prototype design pattern? Following example explains, C++ conversion operator. #include <iostream> #include <string> using...
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.