473,382 Members | 1,635 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,382 software developers and data experts.

conversion operator question

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)
{
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)(double);

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 2136
* hunter hou:
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)
{
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)(double);

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
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
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
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...
16
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,...
7
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...
47
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...
10
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
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) :...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.