| | |
Operator precedence - overloading - (order of things)
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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()
The solution prototype for the overloaded + is this:
Originally, my prototypes were:
For reference here are the constructors and overloaded = prototypes;
I hope this is a clear explanation, short of posting all the code files.
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()
cpp Syntax (Toggle Plain Text)
... 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:
C++ Syntax (Toggle Plain Text)
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;
cpp Syntax (Toggle Plain Text)
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.
Last edited by superjacent; Feb 27th, 2008 at 7:40 am. Reason: formatting.....
•
•
Join Date: Feb 2008
Posts: 3
Reputation:
Solved Threads: 1
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
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
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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
an anonymous String is constructed and passed to
> Originally, my prototypes were ...
these are just fine and for something like
the creation of an anonymous temporary String object is avoided.
> 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;
The compiler complained when seeing
"conversion from `const char[9]' to non-scalar type `String' requested"
Changing all similar lines to
Could someone explain what's happening here. If retaining the 'explicit' constructor is it possible to compile code
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"; . •
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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.
![]() |
Similar Threads
- Database manipulation with AVL tree.. please help (Java)
- Order of precedence (C)
- Logical operations and data-checks (Java)
Other Threads in the C++ Forum
- Previous Thread: inventory Assignment
- Next Thread: help me in compilation
Views: 1379 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






