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.

8 Years
Discussion Span
Last Post by Museful

Hi ,

I think u r problem is when u r adding s2= " Your String " + s2; then this stmt works only when :
1. Overloading of + should be there
2. Overloading should be done through friend function and when we overload any operator through friend function then we have to pass all arguments which are required by an operator in case of + there are 2 operands so during execution of that stmt if converts u r string const " Your String " into an object via one argument constructor and then it add so our third requirement is constructor.
3. One argument constructor which takes const char* and return an object of calss string.

May it helps u.
Let me know
Nishant Soni


> 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.
the String class has a (non-explicit) single argument constructor which takes a const char * . this also acts as an implicit conversion operator.
an anonymous String is constructed and passed to String operator+(const String &s1, const String &s2); > Originally, my prototypes were ...
these are just fine and for something like s2 = "My name is " + s3; is more efficient.
the creation of an anonymous temporary String object is avoided.


Thanks all for responding, very much appreciated and it's certainly cleared the air for me. I've had to go back a chapter and re-read up on implied constructor conversions. I'm now left with this little query.

To turn-off implied constructor conversion I noticed another little side-effect. I used the keyword 'explicit' like so; explicit String(const char * S);// constructor for the constructor prototype.

The compiler complained when seeing String s2 = "Whatever"; with this error message;

"conversion from `const char[9]' to non-scalar type `String' requested"

Changing all similar lines to String s2("Whatever"); solved the problem.

Could someone explain what's happening here. If retaining the 'explicit' constructor is it possible to compile code String s2 = "whatever"; .


String s2 = <some_expression> ; initializes one String with another String (copy construct). if <some_expression> is not a String, it must be implicitly convertible to one. it is semantically like int i = <some_expression> ; initializes one int with another int. if <some_expression> is not an int it is implicitly converted to one.
so, to be able to write String s2 = "Whatever"; requires a non-explicit constructor and an accessible copy constructor ( 'even if it may not be called' ).
to write String s2(" Whatever" ) ; or String s2 = String( "Whatever" ) ; does not require the constructor to be non-explicit.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.