function overloading

this is what i did in c++ while overloading a function

int add(void); //function call with void as argument
int add(); //passing nothing

i know these two lines are perfectly valid. but how? please tell me difference between (void) and ().how compiler is distinguishing between these two arguments and allows overloading the function?
The function declarations int add(void); and int add(); are equivalent. They both mean "there is a function, called add, returning an int that takes no parameters".

how compiler is distinguishing between these two arguments and allows overloading the function?

It doesn't. The following will produce a compiler error because the function already exists:

1
2
3
4
5
6
7
8
9
int add()
{
   return 0;
}

int add(void)
{
   return -1;
}


The following code will not produce compiler errors:

1
2
int add();
int add(void);

because they are simply declarations (and you can have multiples of the same declaration). However, you cannot have multiple definitions.
Last edited on
cool!....
Topic archived. No new replies allowed.