473,581 Members | 3,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

conversion operator question

Hello,Please look at the following code(from C++ in a nutshell) and my
questions.Thank s,***Hunter...
typedef void (*strproc)(cons t char*);

void print(const char* str)
{
std::cout << "const char*:" << str << '\n';
}

void print(int x)
{
std::cout << "int:" << x << '\n';
}

void print(double x)
{
std::cout << "double:" << x << '\n';
}
struct simple

{
void operator( )(int x) { print(x); } // print(int)
void operator( )(double x) { print(x); } // print(double)
};

typedef void (*intfunc)(int) ;
typedef void (*dblfunc)(doub le);

struct indirect
{
operator intfunc( ) { return print; } // print(int)
operator dblfunc( ) { return print; } // print(double)
operator strproc( ) { return print; } // print(const char*)
};

int main( )
{

simple sim;
indirect ind;
sim(42); // Prints "int:42"

sim.operator( )(42); // Prints "int:42"

sim(42.0); // Prints "double:42"//sim(X) is using
overloaded operator ()

ind(42); // Prints "int:42"

ind.operator intfunc( )(42); // Prints "int:42"

ind(42.0); // Prints "double:42"

ind("forty-two"); // Prints "const char*:forty-two"
}Question:The above conversion coperators are used to convert indirect to
intfunc, dblfunc,strproc , how does the above "ind" use coversion operators
to do overloading?and how to decide parameter type?
Feb 10 '06 #1
1 2160
* hunter hou:
Hello,Please look at the following code(from C++ in a nutshell) and my
questions.Thank s,***Hunter...
typedef void (*strproc)(cons t char*);

void print(const char* str)
{
std::cout << "const char*:" << str << '\n';
}

void print(int x)
{
std::cout << "int:" << x << '\n';
}

void print(double x)
{
std::cout << "double:" << x << '\n';
}
struct simple

{
void operator( )(int x) { print(x); } // print(int)
void operator( )(double x) { print(x); } // print(double)
};

typedef void (*intfunc)(int) ;
typedef void (*dblfunc)(doub le);

struct indirect
{
operator intfunc( ) { return print; } // print(int)
operator dblfunc( ) { return print; } // print(double)
operator strproc( ) { return print; } // print(const char*)
};

int main( )
{

simple sim;
indirect ind;
sim(42); // Prints "int:42"

sim.operator( )(42); // Prints "int:42"

sim(42.0); // Prints "double:42"
//sim(X) is using overloaded operator ()

ind(42); // Prints "int:42"

ind.operator intfunc( )(42); // Prints "int:42"

ind(42.0); // Prints "double:42"

ind("forty-two"); // Prints "const char*:forty-two"
}
Be aware that this code is contrived to illustrate a technical aspect of
the language.

Nobody would write code like that to solve some actual problem.

Most of us (including me) would have to compile it to be sure it's
technically OK.

Question:The above conversion coperators are used to convert indirect to intfunc, dblfunc,strproc ,
Yes.
how does the above "ind" use coversion operators
to do overloading?
One might argue that the conversion operators are overloaded on result
type, which is no-no for ordinary functions. However, conversion
operators are not ordinary functions. They are _special_ member
functions, obeying special rules (other special member functions include
constructors, destructors and assignment operators); special member
functions are discussed in section 12 of the standard, "Special member
functions", and conversion operators in 12.3.2.

and how to decide parameter type?


That's something at least I don't want to think about; the rules for
viable functions and matching are complex, to say the least. However,
usually they produce the intuitive result, as they're designed to. Some
times you get surprised, though, and then the best course is in my
opinion to rewrite the code so that there is no (human-side) ambiguity.


--
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?
Feb 10 '06 #2

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

Similar topics

3
1919
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 >) { }
4
3208
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
5
3026
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:
16
2974
by: frs | last post by:
See example below: Why does the output of 'a' work and the output of 'b' fails to compile? Is there a way to write class 'something' so that 'b' converts correctly by default? (include iostream, string, use namespace std) template <typename T> struct something { T x; operator T();
7
3255
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But I couldn't find the exact rule in the C++ standard. And what if the classes have template parameters? It would be great if somebody could get...
47
2846
by: rawCoder | last post by:
Hi, Just wanted to know if there is any speed difference between VB conversion Keywords like CInt, Clng, CStr, CDbl, CBool etc. ..NETs Convert.To<...> methods. And which is better to be used and why ? Thanx
10
1462
by: Jeroen | last post by:
Hi guys, Just another question. Suppose I have 2 classes (incomplete code): class A { A(const B& b); A& operator = (const A& a); }; class B {
4
2097
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) : x0(a), y0(b),
4
2016
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 { // ...
0
7861
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...
0
7789
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8141
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. ...
0
8167
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...
0
6548
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...
0
3817
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2299
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
1
1399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1130
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...

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.