Unexpected problems concatenating strings
c++, c++11, string
Solution
A `std::string` can be added to anything (another `std::string`, double-quoted string literal, `char`) and provide intuitive results, but if you try to add a double-quoted string literal to another string literal or a `char` then it won't "work":
a string literal added to a `char` or other integral value will undergo Standard Conversion to a `const char*`, then any number added to the pointer will move along the literal that number of characters: if the offset isn't inside the string literal then you get undefined behaviour if you dereference (use) the resultant pointer,
two string literals just can't be added, even after decaying to two pointers, so you'll get a compile-time error.
Sometimes you will want to explicitly construct a `std::string` so that concatenation with other values works as you'd like: e.g. `my_string = std::string("hello ") + my_const_char_ptr + '\n'`.
Examples:
std::string s = "Hello";
s + " World"; // ok
"Hello " + s; // ok
"Hello " + "World"; // NOT ok - two string literals
s += " World"; // ok
s += " Goodbye " + "World"; // NOT ok - "+" evaluated before "+="
s += std::string(" Goodbye ") + "World"; // OK - right-hand-side no longer two literals
// BUT may be slower than two "s +="
Problem
I'm trying to concatenate strings using +, but there is some weird stuff going on. Here is my "Grade" class I have for a class project: ``` #pragma once #include <fstream> #include <iostream> #include <string> #include <vector> using namespace std; class Grade { private: string className; string student; string letter; public: Grade(string c, string s, string l) : className(c), student(s), letter(l) {} string getLetterGrade() const { return letter; } string getClassName() const { return className; } string getStudent() const { return student; } void setLetterGrade(string l) { letter = l; return;} void setClassName(string c) { className = c; return;} void setStudnet(string s) { student = s; return;} string toString() const { string output = "hello"+student; return output; } }; ``` Obviously, the toString() method isn't currently what I want it to be. If I run the toString() as above, I get "hello529173860" out, as expected. However, if I change the line to: ``` string toString() const { string output = student+"hello"; return output; } ``` then the output is "hello3860". This isn't just putting the hello string on the front, but its replacing characters from the student string in the process... somehow? Furthermore, if I try to use: ``` string toString() const { string output = "hello"+" world"; return output; } ``` I get an error: ``` Grade.h: In member function ‘std::string Grade::toString() const’: Grade.h:29:53: error: invalid operands of types ‘const char [6]’ and ‘const char [7]’ to binary ‘operator+’ string toString() const { string output = "hello"+" world"; return output; } ^ ``` I'm really at a loss for what it going on here... especially since I have done string concatenation earlier in the program without issue. What I would like is to output something like: "student+[some white space]+letter+[some white space]+className"