By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

Ambig

P: n/a

class A
{
public:

void fun(int* p)
{
cout<<"fun(char* )"<<p<<endl;
}

void fun(int i)
{
cout<<"f(int)"<<i<<endl;
}
};

void main()
{
A a;
a.fun(NULL);
}

Why my function call i.e a.fun(NULL) always calls fun(int), but not
fun(int* i)??
Is it not the ambiguity..

Apr 11 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Radde wrote:
class A
{
public:

void fun(int* p)
{
cout<<"fun(char* )"<<p<<endl;
}

void fun(int i)
{
cout<<"f(int)"<<i<<endl;
}
};

void main()
{
A a;
a.fun(NULL);
}

Why my function call i.e a.fun(NULL) always calls fun(int), but not
fun(int* i)??
Is it not the ambiguity..

Check for your implementation's definition of NULL. You will most
likely find it's 0.

--
Ian Collins.
Apr 11 '06 #2

P: n/a
Radde wrote:
class A
{
public:

void fun(int* p)
{
cout<<"fun(char* )"<<p<<endl;
}

void fun(int i)
{
cout<<"f(int)"<<i<<endl;
}
};

void main()
main() must return int. Your program has undefined behaviour.
{
A a;
a.fun(NULL);
}

Why my function call i.e a.fun(NULL) always calls fun(int), but not
fun(int* i)?? Is it not the ambiguity..


Actually, NULL is not defined at all in your program, just like cout and
endl. Maybe you forgot to copy some #include lines to your posting?
Anyway, the NULL defined in the C++ standard library is a macro that expands
to an integer. So the version taking an int is chosen, because it's a
better match. Generally, you should avoid using NULL for this reason (among
others).

Apr 11 '06 #3

P: n/a
HI,
But macros doesnt check data type..
#define NULL 0

The above macro could be resolved to anything, i mean int, short,
char*, etc..
But how come above macro expands to integer.

Apr 11 '06 #4

P: n/a
Radde wrote:
HI,
But macros doesnt check data type..
That's right. It will just replace any occurance of the macro's name with
what follows after it.
#define NULL 0
So wherever you write NULL, it's exactly the same as if you wrote 0.
The above macro could be resolved to anything, i mean int, short,
char*, etc..
But how come above macro expands to integer.


Because that's what 0 is. Your call

a.fun(NULL);

is equivalent to:

a.fun(0);

and since 0 is an integer, the int version of your function is chosen,
because it needs no conversion. The other overload would require a
conversion to pointer.
Apr 11 '06 #5

P: n/a
Rolf Magnus wrote:

main() must return int. Your program has undefined behaviour.


It's actually a constraint violation, so it requires a diagnostic. The
program is ill-formed.

--

Pete Becker
Roundhouse Consulting, Ltd.
Apr 11 '06 #6

P: n/a
Radde wrote:
But macros doesnt check data type..
Doesn't matter. This macro expands to an integer constant.
#define NULL 0

The above macro could be resolved to anything, i mean int, short,
char*, etc..


No, the type of 0 is int.

--

Pete Becker
Roundhouse Consulting, Ltd.
Apr 11 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.