473,396 Members | 1,799 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.

conversion operators (operator keyword)

Example...

This would make that i can assign a ULONG to a CComBSTR2 class while the
body of the code performs necessary conversion.

CComBSTR2& operator=(ULONG ulong)

{

}

QUESTION: Can I do it the other way around as seen below?

// code below does compile but still has no effect.

ULONG& operator=(const &CComBSTR2)

{

}

Nov 17 '05 #1
3 1282
Egbert Nierop (MVP for IIS) wrote:
Example...

This would make that i can assign a ULONG to a CComBSTR2 class while
the body of the code performs necessary conversion.

I'm going to assume that you meant both of these examples to be members of
CComBSTR2...

CComBSTR2& operator=(ULONG ulong)
{
}

QUESTION: Can I do it the other way around as seen below?

// code below does compile but still has no effect.
ULONG& operator=(const &CComBSTR2)
{
}


The problem is that if you write

CComBSTR2 bs2;
ULONG ul;

ul = bs2;

The compiler will never look for an overload of operator= in the CComBSTR2
class. It will only search the "associated namespaces" and the ULONG class
itself. Of course, ULONG is a built-in, so all of that is moot, as it's
impossible to overload any operators for a built-in type (i.e. make
operators that behave as if they're member functions of the built-in type).

What you want instead is a conversion operator

operator ULONG()
{
// convert your string to ULONG
}

Then your assignment to ULONG will succeed.

Generally this kind of conversion function is discouraged - having an
implicit conversion to an integer type will cause many code constructs that
were actually errors to be legal code.

Instead, it's generally preferred that you write an explicit conversion:

ULONG ToULong()
{
// convert your string to ULONG
}

and call it explicitly when you need to assign to a ULONG.

-cd

Nov 17 '05 #2
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uJ**************@tk2msftngp13.phx.gbl...
Egbert Nierop (MVP for IIS) wrote:
Example...

This would make that i can assign a ULONG to a CComBSTR2 class while
the body of the code performs necessary conversion.

I'm going to assume that you meant both of these examples to be members of
CComBSTR2...

Your suggestion was OK. thanks.
CComBSTR2& operator=(ULONG ulong)
{
}

QUESTION: Can I do it the other way around as seen below?

// code below does compile but still has no effect.
ULONG& operator=(const &CComBSTR2)
{
}


The problem is that if you write

CComBSTR2 bs2;
ULONG ul;

ul = bs2;

The compiler will never look for an overload of operator= in the CComBSTR2
class. It will only search the "associated namespaces" and the ULONG
class itself. Of course, ULONG is a built-in, so all of that is moot, as
it's impossible to overload any operators for a built-in type (i.e. make
operators that behave as if they're member functions of the built-in
type).

What you want instead is a conversion operator

operator ULONG()
{
// convert your string to ULONG
}

Then your assignment to ULONG will succeed.

Generally this kind of conversion function is discouraged - having an
implicit conversion to an integer type will cause many code constructs
that were actually errors to be legal code.

Instead, it's generally preferred that you write an explicit conversion:

ULONG ToULong()
{
// convert your string to ULONG
}

and call it explicitly when you need to assign to a ULONG.

-cd


Nov 17 '05 #3
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uJ**************@tk2msftngp13.phx.gbl...
Egbert Nierop (MVP for IIS) wrote:
Example...

This would make that i can assign a ULONG to a CComBSTR2 class while
the body of the code performs necessary conversion.

I'm going to assume that you meant both of these examples to be members of
CComBSTR2...

Your suggestion was OK. thanks.
CComBSTR2& operator=(ULONG ulong)
{
}

QUESTION: Can I do it the other way around as seen below?

// code below does compile but still has no effect.
ULONG& operator=(const &CComBSTR2)
{
}


The problem is that if you write

CComBSTR2 bs2;
ULONG ul;

ul = bs2;

The compiler will never look for an overload of operator= in the CComBSTR2
class. It will only search the "associated namespaces" and the ULONG
class itself. Of course, ULONG is a built-in, so all of that is moot, as
it's impossible to overload any operators for a built-in type (i.e. make
operators that behave as if they're member functions of the built-in
type).

What you want instead is a conversion operator

operator ULONG()
{
// convert your string to ULONG
}

Then your assignment to ULONG will succeed.

Generally this kind of conversion function is discouraged - having an
implicit conversion to an integer type will cause many code constructs
that were actually errors to be legal code.

Instead, it's generally preferred that you write an explicit conversion:

ULONG ToULong()
{
// convert your string to ULONG
}

and call it explicitly when you need to assign to a ULONG.

-cd


Nov 17 '05 #4

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

Similar topics

3
by: BigMan | last post by:
Here is a piece of code: #include <memory> using namespace std; template< typename SomeType > void f(auto_ptr_ref< SomeType >) { }
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...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
3
by: Chris | last post by:
Hi, I want to convert an integer to an object of type 'MyNumber' using implicit conversions. MyNumber* pDt = 10; but it doesn't work ?? Here's what I did :
4
by: Påhl Melin | last post by:
I have some problems using conversion operators in C++/CLI. In my project I have two ref class:es Signal and SignalMask and I have an conversion function in Signal to convert Signal:s to...
1
by: hunter hou | last post by:
Hello,Please look at the following code(from C++ in a nutshell) and my questions.Thanks,***Hunter... typedef void (*strproc)(const char*); void print(const char* str) { std::cout << "const...
14
by: Richard G. Riley | last post by:
Would it be wrong to use "implicit casting" instead of the standards "implicit conversion" when talking about implicit conversions between certain data types. The standard mentions "explicit...
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...
7
by: Fraser Ross | last post by:
I was looking at the new explicit conversion operators in the standard. The writing at the start of 12.3.2/2 doesn't make sense. "A conversion function may be explicit (7.1.2), in which case it is...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.