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

Help needed to understand "member function template"

I am reading the book "Effective C++" Item 25 mentioned the following
code:
// a first cut at a class yielding NULL pointer objects
class NullClass {
public:
template<class T // generates
operator T*() const { return 0; } // operator T* for
}; // all types T; each
// function returns
// the null pointer
const NullClass NULL; // NULL is an object of
// type NullClass
void f(int x); // same as we originally had
void f(string *p); // ditto
f(NULL); // fine, converts NULL to
// string*, then calls f(string*)
I have a hard time to understand the above, can anybody explain it to
me? thanks,

Sep 22 '07 #1
3 1459
my understanding is like this:
compiler would try to find out the right function for call f(NULL),
and will try f(int x), obviously this is not the right one, then will
try f(string *P), but this definition needs pointer so compiler will
try to call T* to do implicit conversion.

Am i right? thanks

Sep 22 '07 #2
Aries Sun wrote:
my understanding is like this:
compiler would try to find out the right function for call f(NULL),
and will try f(int x), obviously this is not the right one, then will
try f(string *P), but this definition needs pointer so compiler will
try to call T* to do implicit conversion.

Am i right? thanks
you are right,
First, it find the exact match, but fail,
then find any other, to which NULL is convertible.

BTW, don't use NULL as an variable name in the program other than demo,
as it is a standard macro.

--
Thanks
Barry
Sep 22 '07 #3
thanks!

On Sep 22, 4:54 am, Barry <dhb2...@gmail.comwrote:
Aries Sun wrote:
my understanding is like this:
compiler would try to find out the right function for call f(NULL),
and will try f(int x), obviously this is not the right one, then will
try f(string *P), but this definition needs pointer so compiler will
try to call T* to do implicit conversion.
Am i right? thanks

you are right,
First, it find the exact match, but fail,
then find any other, to which NULL is convertible.

BTW, don't use NULL as an variable name in the program other than demo,
as it is a standard macro.

--
Thanks
Barry

Sep 22 '07 #4

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

Similar topics

2
by: Andy Norman | last post by:
I have a strange problem. When I try to call the input property of the MSXML processor object from VBScript in an ASPX page I get the error "Member not found". The same code (with a few "set"...
4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
0
by: Gianni Mariani | last post by:
I remember seeing a neat template specialization trick posted here a few months ago that allowed the detection of a type containing a member. After a day searching through google archives I come up...
10
by: Lionel B | last post by:
Greetings, I cannot figure out why the following code does not compile: --- BEGIN CODE --- typedef double ftype(double); struct A {
6
by: Jef Driesen | last post by:
I need to implement a function to implement the rounding of floating point values. At the moment i have two different implementations, depending on the type of the return value (integer or double)....
9
by: Kobe | last post by:
Is there any difference in: template <class T> vs. template <typename T> ?
5
by: Gianni Mariani | last post by:
I'm hoping someone can tell me why using member address of works and why using the dot operator does not in the code below. The code below uses the template function resolution mechanism to...
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
4
by: viki | last post by:
We have zillion of instances inf instance->m_member in the code. We are going introduce the 'accessors' Get() and Set() for m_member, and m_member going private (and renamed, too). We would like...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...

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.