Reputation: 1365
I have an example of two test programs I wrote in C++. The first one works fine, the first one errors. Please help me to explain what is going on here.
#include <iostream>
#include <string>
#include <stdint.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
string randomStrGen(int length) {
static string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
string result;
result.resize(length);
for (int32_t i = 0; i < length; i++)
result[i] = charset[rand() % charset.length()];
return result;
}
int main()
{
ofstream pConf;
pConf.open("test.txt");
pConf << "rpcuser=user\nrpcpassword="
+ randomStrGen(15)
+ "\nrpcport=14632"
+ "\nrpcallowip=127.0.0.1"
+ "\nport=14631"
+ "\ndaemon=1"
+ "\nserver=1"
+ "\naddnode=107.170.59.196";
pConf.close();
return 0;
}
It opens 'test.txt' and writes the data, no problem. This, however, does not:
#include <iostream>
#include <string>
#include <stdint.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
string randomStrGen(int length) {
static string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
string result;
result.resize(length);
for (int32_t i = 0; i < length; i++)
result[i] = charset[rand() % charset.length()];
return result;
}
int main()
{
ofstream pConf;
pConf.open("test.txt");
pConf << "rpcuser=user\n"
+ "rpcpassword="
+ randomStrGen(15)
+ "\nrpcport=14632"
+ "\nrpcallowip=127.0.0.1"
+ "\nport=14631"
+ "\ndaemon=1"
+ "\nserver=1"
+ "\naddnode=107.170.59.196";
pConf.close();
return 0;
}
The only difference in the second program is that 'rpcpassword' has been moved to the next line.
matthew@matthew-Satellite-P845:~/Desktop$ g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:23:6: error: invalid operands of types ‘const char [14]’ and ‘const char [13]’ to binary ‘operator+’
+ "rpcpassword="
Upvotes: 0
Views: 224
Reputation: 3202
"rpcuser=user\nrpcpassword=" + randomStrGen(15) + "\nrpcport=14632"
groups like ("rpcuser=user\nrpcpassword=" + randomStrGen(15)) + "\nrpcport=14632"
. Here, +
is always used with an argument of a class type, so you get std::string::operator+
after overload resolution.
"rpcuser=user\n" + "rpcpassword=" + randomStrGen(15)
groups like ("rpcuser=user\n" + "rpcpassword=") + randomStrGen(15)
. In this case, the first +
is used on two non-class types, so it's not overloaded and the language doesn't define +
for two const char []
values. (I come from old C, so I was a bit it didn't just add them as char *
s and give you a nice SIGSEGV at runtime.)
Upvotes: 4
Reputation: 37940
A string literal ("foo"
) in C++ is not of the type string
; it is of the type const char[x]
, where x
is the length of the string literal plus 1. And character arrays can't be joined with +
. However, a character array can be joined with a string
, and the result is a string
, which may further be joined with character arrays. Therefore, "a" + functionThatReturnsString() + "b"
works, but "a" + "b"
does not. (Keep in mind that +
is left associative; it is applied to the two leftmost operands first, and then to the result and the third operand, and so on.)
Upvotes: 5