Help needed to understand "member function template"

A

Aries Sun

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,
 
A

Aries Sun

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
 
B

Barry

Aries said:
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.
 
A

Aries Sun

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top