This is a general question about parentheses in c++.
Whilst creating a copy of a string, i wanted to initialize a new instance of a string which then gets passed as the parameter to the constructor.
Here is what i tried (example of a string copy):
const char* test_str = "hello world";
std::string copied_str(std::string(test_str));
Corresponding output of copied_str
:
1
After wrapping the initialization in parentheses, it works just as intended.
Example with parentheses:
const char* test_str = "hello world";
std::string copied_str((std::string(test_str)));
Corresponding output of copied_str
with parentheses:
hello world
Why is the compiler not able to execute the first example as intended without parentheses, and what exactly is the parentheses doing in the second example?
Additional question which just interests me. Why is the output of the first example 1
?