I made several examples to learn about function overload. Next example compiles, but with a warning:
$ g++ overloadF.cpp -c
overloadF.cpp: In function ‘int main()’:
overloadF.cpp:19:24: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
pd.print("Hello C++");
#include <iostream>
usingnamespace std;
class printData
{
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(char* c) {
cout << "Printing characters: " << c << endl;
}
};
int main(void)
{
printData pd;
pd.print(5);
pd.print("Hello C++");
return 0;
}
I find this puzzling. I call pd.print with a string, which is an array of chars. So if i pass it, the value passed is a pointer (to the first element of the array); therefor char* c should be correct as argument. cout will then print the array from *c and raise the pointer and print char values, up until '\0' is found. Well it does.
String literals "like this" are of type charconst *, but you are using a non-standard compiler extension that allows you to (illegally) implicitly convert it to char *, which casts away the const (a very bad thing).