Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2005

Tip: Looking for answers? Try searching our database.

conversion operators (operator keyword)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Egbert Nierop \(MVP for IIS\) - 25 Jun 2005 09:44 GMT
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)

{

}
Carl Daniel [VC++ MVP] - 25 Jun 2005 14:36 GMT
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)
> {
[quoted text clipped - 6 lines]
> {
> }

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
Egbert Nierop \(MVP for IIS\) - 27 Jun 2005 21:10 GMT
> Egbert Nierop (MVP for IIS) wrote:
>> Example...
[quoted text clipped - 4 lines]
> 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)
>> {
[quoted text clipped - 44 lines]
>
> -cd

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.