Вы можете написать свою функцию, чтобы взять const std::string&
:
void print(const std::string& input)
{
cout << input << endl;
}
или const char*
:
void print(const char* input)
{
cout << input << endl;
}
Оба способа позволяют назвать это так:
print("Hello World!\n"); // A temporary is made
std::string someString = //...
print(someString); // No temporary is made
Вторая версия требует c_str()
для вызова std::string
s:
print("Hello World!\n"); // No temporary is made
std::string someString = //...
print(someString.c_str()); // No temporary is made