See these two overloads of a function F :-
void F()
{
cout << "F()" << endl;
}
void F(int i = 10)
{
cout << "F(int) " << i << endl;
}
Now if you attempt to call the function as F() you’ll get a compiler error.
/* F(); <- C2668: 'F' : ambiguous call to overloaded function */
The right way to do this would be :-
((void (*)(void))F)(); ((void (*)(int = 10))F)();
Looks beautiful, doesn’t it? C++ syntax is truly the most beautiful among all programming languages!
Is this part of C++ standard? Or MS compiler specific?
I think you should redefine your definition of beauty. This is the problem with C++ syntax, it’s too complicated.
Hello
Shouldn’t the compiler understand the difference? or am I confusing
this with something else?!!! isn’t this polymorphism?
Huh, I would expect the second version of F() to be illegal right off the bat,
since it creates ambiguity with the first one.
interesting!
Did you try?
void F (void);
void F(void)
{
cout
Hi,
This seems to be interesting… how compiler resolves the ambiguity… Still, even if this
compiles… Which function will get called for F()…
Thanks –
Nish, the only reason you are getting an error here is that the declaration “void F()” resolves to “void F(int)” for non-member functions. This is a legacy ferature for C compatibility, and one which PC-Lint will warn you about if you run code written this way through it.
By the way, another thing that will result in a PC-Lint message (#1924, assuming your warning policy has it enabled) is the use of C-style casts as you’ve advocated.
Effective C++” discusses why (although I would hope it would be self evident). I *always* use C++ style casts rather than C style casts in new code as a result.
void F()
{
int i = 10;
// use i for something
}
void F(int i = 10)
{
// use i for something
}