As an exercise on function overloading concept, I was playing with the following sketch in UNO for adding two interger numbers and two floating point numbers with the same function name (add). Unfortunately, the sketch works well with double type for the float numbers but not with float type. Would appreciate to hear the reasons from the Forum.
This is the compilation error message: call of overloaded 'add(double, double)' is ambiguous
Why does my sketch work without the use of the overload keyword?
The textbook says:
"The C++ compiler must be told in advance, using the keyword overload, that a function is going to be overloaded." Accordingly I tried to add the following line in the global space; however, it is not accepted by the compiler and eventually, the sketch works without this line.
template<typename T>
const T add( const T& A, const T& B ) { // for simple numbers A and B you can get rid of the &
return A + B;
}
void setup()
{
Serial.begin(9600);
int resint = add(8, 5);
float resf = add(3.41, 7.54);
Serial.println(resint);
Serial.println(resf, 2);
}
void loop() {}
Your trick works well without appending f with the actual arguments.
Can you please, briefly describe the working principle of the following function? I understand that the function is receiving the addresses of the actual arguments from the calling function. After that what happens?
template<typename T>
const T add( const T &A, const T &B )
{ // for simple numbers A and B you can get rid of the &
return A + B;
}
forget the & for the time being, that's just if A and B were using a more complex type like an instance of a matrix etc, to avoid creating a copy of the instance and work on the original 'by reference')
what you define is called a template. You tell the compiler to build on demand a function when needed based on the types used in the code.
for example when you call add(8, 5); the compiler sees you need to call the function with 2 int. It matches the template (the conceptual type is 'T' which will be replaced by int) and so the compiler generates and compile
const int add( const int A, const int B ) {
return A + B;
}
then you call add(3.41, 7.54). here the compiler sees two double (because you did not use the f suffix) and so generates and compile
const double add( const double A, const double B ) {
return A + B;
}
and this will be the function called.
➜ the template lets you define basically all the overloaded variant in one go and you let the compiler decide which one makes sense to generate
I'm sure a lot of the examples you find in there won't even compile now at least not under C++ For example this:
double old_style(); /* Obsolete function declaration */
double alt_style( a , real ) /* Obsolete function definition */
double *real;
int a;
{
return ( *real + a ) ;
}
I have stack of IT books of that vintage on the bookshelves and the only reason i don't throw them away is to prevent my wife misusing the space for something else.
In 1991, that was the hard copy book for me to practice and understand some of the syntax and semantic rules of the C Language (course work) under Turbo C . Now-a-days, there are huge tutorials in the net from various sources and yet I consult that book time-to-time as it contains few definitions of my liking -- for example: Constructor Function.