depcrated convers str to char*

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++");

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace 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.

But where do i go wrong?
Thanks for your help.
String literals "like this" are of type char const *, 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).
Last edited on
Thanks. Probably this comes to early for me.

[ But okay forgive me if i'm wrong, somestring="Hallo" ; is an array of chars with pointer somestring can/may not be changed? ]
You can copy a string literal into a non-const character array, but you can't legally have a pointer to non-const characters to a string literal.
Topic archived. No new replies allowed.