Hope someone can clarify. I'm self-studying and my question relates to Programming Exercise # 2, Chapter 12, C++ Primer Plus Fifth Edition (pg. 629). I completed the exercise and achieved the desired result but my query came about when I compared my version to the solution.
The exercise builds upon a class (home grown String class) which was created early in the chapter. The task was to create additional class member methods so as the given main() program would work. That is, main() was already created.
My query surrounds overloading the + operator in order to join two strings.
Here is a part of main()
... String s1(" and I am a C++ student."); String s2 = "Please enter your name: "; String s3; cout << s2; // overloaded << operator cin >> s3; // overloaded >> operator s2 = "My name is " + s3; // overloaded = , + operators cout << s2 << ".\n"; s2 = s2 + s1; ...
The solution prototype for the overloaded + is this:
friend String operator+(const String &s1, const String &s2); My question stems from line # 7
s2 = "My name is " + s3; . How is that the "My Name.." bit, which I believe to be a const char* or string literal be somehow resolved to a String object for the purposes of the function.
Originally, my prototypes were:
String operator+(const String &s1); friend String operator+(const String &s, const char *c); friend String operator+(const char *c, const String &s);
For reference here are the constructors and overloaded = prototypes;
String(const char * S); // constructor String(); // default constructor ~String(); // destructor String(const String &sb); // copy constructor String &operator=(const String &st); String &operator=(const char *);
I hope this is a clear explanation, short of posting all the code files.