different types without template
Apr 12, 2014 at 9:54pm UTC
Hi, I am wondering how can I make something like this:
1 2 3 4 5 6 7 8 9
int check()
{
do something...........
}
double check()
{
do something..........
}
My teacher says that I cannot use template in the assignment.
So how can I do it?
Apr 12, 2014 at 10:01pm UTC
You can overload functions, the compiler uses the number and type of the arguments and whether the function is const
(for class functions) to determine which version of the function to execute.
Does this help?
Apr 13, 2014 at 1:40am UTC
so what you mean is I can do this?
1 2 3 4 5 6 7 8 9
int dothis(int temp)
{
}
double dothis(double temp)
{
}
and depends what type put for temp, it will get in that function?
Apr 13, 2014 at 7:01am UTC
yes.
Also:
1 2 3 4
double dothis(double temp1, double temp2){}
double dothis(double temp1, int temp2){}
double dothis(double temp1, char temp2){}
double dothis(double temp1, double temp2, double temp3){}
As I said, along as the number & type of the arguments produces a unique combination.
Overloading applies to constructors, destructors, and operators too.
Good Luck :+)
Apr 13, 2014 at 9:47am UTC
Just a note: overloading resolution will take into account only function arguments, not return value. That means you cannot do that:
1 2
double dothis(int x);
int dothis(int x);
Topic archived. No new replies allowed.