473,486 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Automatic type conversion.

I want Foo-type objects to be convertible to Bar-type objects. Can I use
operator Bar() to do this just as I would use operator bool() to convert to
boolean? Example:

class Foo
{
public:
operator Bar() const;
};

Thanks.
Jul 22 '05 #1
4 2403
* Jason Heyes:
I want Foo-type objects to be convertible to Bar-type objects. Can I use
operator Bar() to do this just as I would use operator bool() to convert to
boolean?


Yes.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
Jason Heyes wrote:
I want Foo-type objects to be convertible to Bar-type objects. Can I use
operator Bar() to do this just as I would use operator bool() to convert
to boolean? Example:

class Foo
{
public:
operator Bar() const;
};


Yes, you can use that. But it is usually better to make a conversion
constructor in Bar, like:

class Bar
{
public:
Bar(const Foo& rhs);
};

Jul 22 '05 #3
> I want Foo-type objects to be convertible to Bar-type objects. Can I use
operator Bar() to do this just as I would use operator bool() to convert to
boolean? Example:

class Foo
{
public:
operator Bar() const;
};


(mmm.. why not just try it?)

Yes you can, but be aware that automatic conversions have their pitfalls
and you may get some surprising results. Automatic conversions reduces
the number of places a compiler can warn against potential logic errors.
Jonathan
Jul 22 '05 #4

"Rolf Magnus" <ra******@t-online.de> skrev i en meddelelse
news:co*************@news.t-online.com...
Jason Heyes wrote:
I want Foo-type objects to be convertible to Bar-type objects. Can I use
operator Bar() to do this just as I would use operator bool() to convert
to boolean? Example:

class Foo
{
public:
operator Bar() const;
};


Yes, you can use that. But it is usually better to make a conversion
constructor in Bar, like:

class Bar
{
public:
Bar(const Foo& rhs);
};

.... and even better to make it explicit (if this is compatible with Jasons
goal):

explicit Bar(const Foo& rhs);

/Peter
Jul 22 '05 #5

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

Similar topics

7
3638
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
7
1812
by: bartek | last post by:
Please consider the following scenario below (sketch of). There are two templates defined: A and B, both with mutual conversion operators defined. Also, there's a free function template...
1
1253
by: Aaron | last post by:
I know now (thanks to this board) that there are numerous ways to force a type conversion from a string to a number (such as +, Number(), adding 1, parseInt(), etc). I'm curious, though...is there...
1
11226
by: Philip Bondi | last post by:
Hello to all SQL Server junkies who work with non-English characters: For people running scripts from the command line using ANSI files with special characters, it is very important to use isql...
7
4207
by: Ben R. | last post by:
How does automatic type casting happen in vb.net? I notice that databinder.eval "uses reflectoin" to find out the type it's dealing with. Does vb.net do the same thing behind the scenes when an...
2
1498
by: Partha Mandayam | last post by:
Is there a tool for asp.net 1.1 to asp.net 2.0 automatic conversion?
3
1239
by: Russ | last post by:
I posted a message on this several days ago, but it apparently got lost in googlespace, so I'll try it again. I recently discovered a bug in my code that apparently resulted from the automatic...
3
1853
by: Markus Dehmann | last post by:
I think this is a question about automatic type conversion, but I didn't find the answer after googling for these words ... I have a class called Value (source see below) which can hold an int...
6
2615
by: Rahul | last post by:
Hi Everyone, I have the following code, class B; class A { public : operator B();
0
7100
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
7126
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
7175
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...
1
6842
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
7330
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
5434
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,...
0
3070
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...
0
1378
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 ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.