473,396 Members | 2,010 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,396 software developers and data experts.

Ambig


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
6 1493
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sony Antony | last post by:
Hello: ( Please redirect me to the correct list if this is not where I m supposed to ask this question ) Our application essentially sends xml 'commands' to another system. These commands...
2
by: Soren Kuula | last post by:
Hi, Does anyone happen to know whether this is legal: <complexType name= ...> <choice> <element ref="foo"/> <element ref="bar"/> </choice> </complexType>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.