473,657 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1306
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.nos pam>
wrote in message news:uJ******** ******@tk2msftn gp13.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.nos pam>
wrote in message news:uJ******** ******@tk2msftn gp13.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
1926
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
3029
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 templatized conversion operator defined as follows:
11
7606
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 Test. I expected the same error from the following line, but it compiles fine. The double is silently truncated to an int and then fed in to the implicit conversion operator. Why does this happen? Is there any way that I can keep the implicit...
3
1077
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
2902
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 SignalMask:s. The reason is I have a free function called WaitSignal that accepts av SignalMask where Signals parameters are supposed to implicitly be converted to SignalMask:s. I'm using the SignalMask class because I want to be able to supply a logic...
1
2162
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 char*:" << str << '\n'; } void print(int x)
14
2218
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 conversion" for a cast operation
4
2017
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 functions that offer conversions instead of conversion operators. In page 87, example 2: Errors that work. class String { // ...
7
2184
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 only considered as a user-defined conversion for direct-initialization (8.5)." That sentence is ok. "Otherwise, user-defined conversions are not restricted to use in assignments and initializations." This one contradicts the other. ...
0
8413
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.