Reputation: 39
I am loosing my mind and new to C++ I know C# where I know that it is as simple as
var cat = "cat";
dvar(0,0, "hi" +cat+ "hi");
My issue here is I am developing a game and need to put a string into a function call like so:
string host = "HIST";
dvar(0,0, "s \"test" + host.c_str() + "connection\"");
Also about the threading I am going nuts because my game I can only call in one function at a time but I have a function that is on scree instructions that has a constant while loop so it's to busy handing that looping thread for me to activate any other functions via buttons.
Upvotes: 0
Views: 121
Reputation: 51253
You should do call the function without the c_str()
in order to use the non-member string concatenation function.
dvar(0,0, ("s \"test" + host + "connection\"").c_str());
Since host
is a std::string
type the +
operator will result in calling the non-member function operator+
for std::string
.
E.g. host + "connection"
will result in calling the following function, where "connection"
is implicitly converted into a std::string
:
std::string operator+(const std::string& lhs, std::string&& rhs);
However, if you would do host.c_str() + "connection"
, the compiler would be looking for a function that looks like:
??? operator+(const char* lhs, const char* rhs);
Which doesn't exist in the standard library.
Upvotes: 1
Reputation: 59811
The expression:
"s \"test" + host.c_str() + "connection\"
will try to add pointers to char
. This cannot work. You might be
looking for a string class?
std::string host = "bar";
// this is quite inefficient
func("foo" + host + "baz");
// this is somewhat better
std::string x = "foo";
x.append(host);
x.append("baz");
func(x);
Upvotes: 0