Operator precedence - overloading - (order of things)

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Operator precedence - overloading - (order of things)

 
0
  #1
Feb 27th, 2008
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()
  1. ...
  2. String s1(" and I am a C++ student.");
  3. String s2 = "Please enter your name: ";
  4. String s3;
  5. cout << s2; // overloaded << operator
  6. cin >> s3; // overloaded >> operator
  7. s2 = "My name is " + s3; // overloaded = , + operators
  8. cout << s2 << ".\n";
  9. s2 = s2 + s1;
  10. ...

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:
  1. String operator+(const String &s1);
  2. friend String operator+(const String &s, const char *c);
  3. friend String operator+(const char *c, const String &s);

For reference here are the constructors and overloaded = prototypes;
  1. String(const char * S); // constructor
  2. String(); // default constructor
  3. ~String(); // destructor
  4. String(const String &sb); // copy constructor
  5. String &operator=(const String &st);
  6. String &operator=(const char *);

I hope this is a clear explanation, short of posting all the code files.
Last edited by superjacent; Feb 27th, 2008 at 7:40 am. Reason: formatting.....
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 3
Reputation: nsoni is an unknown quantity at this point 
Solved Threads: 1
nsoni nsoni is offline Offline
Newbie Poster

Re: Operator precedence - overloading - (order of things)

 
0
  #2
Feb 27th, 2008
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
Thanks
Nishant Soni
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Operator precedence - overloading - (order of things)

 
0
  #3
Feb 27th, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: Operator precedence - overloading - (order of things)

 
0
  #4
Feb 27th, 2008
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"; .
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Operator precedence - overloading - (order of things)

 
0
  #5
Feb 28th, 2008
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.
Last edited by vijayan121; Feb 28th, 2008 at 4:38 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: Operator precedence - overloading - (order of things)

 
0
  #6
Feb 28th, 2008
Thanks everyone, much appreciated.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1379 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC