How can I properly pass a const string, for example: "Hello" as a default parameter to a constructor without getting any warnings and errors? In other words how can I preserve memory for them before the function is called?
Asked
Active
Viewed 2,861 times
-2
-
1What warnings/errors you are talking about? – arrowd Mar 09 '17 at 06:15
-
@arrowd One major one is:C:...\main.cpp|26|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]| – Friendly Fella Mar 09 '17 at 06:16
-
by the way i need the char* cos it's part of our homework – Friendly Fella Mar 09 '17 at 06:19
-
1Use `const char*` – user Mar 09 '17 at 06:23
1 Answers
1
#include <iostream>
const char *defString = "Hello";
void foo(const char *str = defString) {
std::cout << str;
}
int main() {
foo();
foo("Hello, world!\n");
}

Zefick
- 2,014
- 15
- 19
-
Is there no other way? What if I want to give the str sth made of 50 characters in the function??? (Without using "new" keyword to allocate memory) – Friendly Fella Mar 09 '17 at 06:38