-2

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?

Friendly Fella
  • 75
  • 1
  • 2
  • 10

1 Answers1

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