[try Beta version]
Not logged in

 
function overloading

Feb 8, 2015 at 3:49am
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?
Feb 8, 2015 at 3:57am
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 Feb 8, 2015 at 3:57am
Feb 8, 2015 at 4:30am
cool!....
Topic archived. No new replies allowed.