overloaded function &default argument
:confused:
#include<iostream.h>
void point();
void point(int x=2,int y=3);
void main()
{
point();
}
1 void point()
{
cout<<"Function"<<endl;
}
2 void point (int /*=2*/,int/*=3*/)
{
cout<<x<<" "<<y<<endl;
}
in this programme we can easily call the "2"function by assign it one or two values ,if assigned one value ,then it will ues the other default argument ,however ,how we call the "1"function ?
[539 byte] By [
jolley] at [2007-11-18 20:33:59]

# 1 Re: overloaded function &default argument
as much as i know, there's no way.
If you do not assign a value, there's no possiblity to decide which function fit's best. The only way i know is to remove the first default argument.
#include<iostream.h>
void point(){
cout<<"Function"<<endl;
}
void point (int x,int y=3){
cout<<x<<" "<<y<<endl;
}
int main(){
point(2);
point();
return 0;
}
# 2 Re: overloaded function &default argument
Surely the example shouldn't compile. You should get an error about ambiguous overloads.
# 3 Re: overloaded function &default argument
FYI:
#include<iostream.h>
iostream.h is not part of the C++ standard, and should not be used in any new code.
Use iostream instead.
#include<iostream>
Using non-standard header makes your code non-portable.
Axter at 2007-11-9 0:35:36 >

# 4 Re: overloaded function &default argument
i am so thankfull to ohgiewahn,
you give me a way out .that is what i neglect in my study .
and axter.i am also happy that you can reply me .but there
is something in your explanation,that is the usage of #include<iostream> and #include<iostream.h>
while i often prefer to the latter .because using the former always leads to some extra writing .such as "using namespace std;"
or i should give each functions in the base a "std::"
which is easy to be overlooked
# 5 Re: overloaded function &default argument
Originally posted by jolley
but there is something in your explanation,that is the usage of #include<iostream> and #include<iostream.h>
while i often prefer to the latter .because using the former always leads to some extra writing .such as "using namespace std;"
or i should give each functions in the base a "std::"
which is easy to be overlooked
Well...as being mentioned the latter one is not a header file supported by the standard and for example are no longer supported with the latest Visual Studio .NET...as for the extra writing...
The following shows you the four different methods to map a namespace...
// Using the full member name, including the namespace it belongs to:
std::cout << "Hello World";
// By taking advantage of Using-Declarations:
using std::cout; // This declares cout in the current
// scope as synonym for std::cout
cout << "Hello World";
// By taking advantage of Using-Directives:
using namespace std; // Which specifies that the current
// scope can refer to names in the
// 'std' namespace without using
// full qualifiers. This is mostly
// used when porting legacy code.
cout << "Hello World";
// Using aliases:
namespace X
{
namespace Y
{
class Z { ... };
}
}
X::Y::Z // The full qualifier for 'Z' is
// 'X::Y::Z'
namespace w = X::Y; // This declares 'w' as an alias for
// namespace 'X::Y'
w::Z // Access 'Z' using 'w::Z'
# 6 Re: overloaded function &default argument
Originally posted by Graham
Surely the example shouldn't compile. You should get an error about ambiguous overloads.
Not quite. Ambiguous overloads are legal, as long as you don't call them. Thus the code should compile, as long as you do not have point() in there.
Have a look at the following example. It compiles and runs cleanly under g++.
#include<iostream>
using namespace std;
void point() {
cout << "point()" << endl;
}
void point (int x = 2,int y = 3) {
cout << "point(" << x << ", " << y << ")" << endl;
}
int main() {
point(3);
point(5,6);
// To call point() one has to use a function pointer
void(*f)() = point;
f();
return 0;
}
# 7 Re: overloaded function &default argument
Nice workaround treuss,
but the problem was and still is - the design.
There is no sense to have identical names to different purposes,
unless it use different information or declared in other contexts.
Guysl at 2007-11-9 0:39:35 >

# 8 Re: overloaded function &default argument
Originally posted by treuss
Not quite. Ambiguous overloads are legal, as long as you don't call them.
The original example posted does call point(), so you should get a complaint about an ambiguous overload.