-
operator overloading
I'm writing my own String class as practice, and I wnated to know something:
I'm overloading operator+ so I could do:
Code:
myString = myString2 + "abcdef";
myString = myString2 + 'k';
myString = myString2 + myString3;
How can I overload this operator so I could do stuff like:
Code:
myString = "abcdef" + myString2
myString = 'p' + myString2
Thanks in advance. :)
-
To add 2 of your classes, you could use:
Code:
strclass& operator+ (strclass &str )
{
strcat(strString, str.strString);
return *this;
}
// Usage: (assuming you overloaded '=')
strclass one;
strclass two;
one = "test";
two = "test";
strclass three = one + two;
-
I know how to do that.
That's not the problem.
The problem is doing:
Code:
String myStr = "abcdef" + myOtherStr;
-
they will automatically cast to the string class if you overload assignment operator
-
I have these operators overloaded so far:
Code:
char & operator [](ulong) const; // String[x] - returns a reference to the char at offset x
const char* operator ()() const; // default operator - Same as GetString()
String& operator =(const String&); // String1 = String2
String& operator =(const char*); // String1 = "abcde"
String& operator =(char); // String2 = 'x'
String operator +(const char*); // String3 = String2 + "abcdef"
String operator +(char); // String3 = String2 + 'k'
String operator +(const String&); // String3 = String2 + String1
When I try to do:
Code:
#include <iostream>
#include "scorpstr.h"
using namespace std;
int main()
{
String myStr1 = "World...";
String myStr4 = "Hello " + myStr1;
cout << myStr4() << endl;
return 0;
}
I get these errors:
Code:
--------------------Configuration: String - Win32 Debug--------------------
Compiling...
main.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\String\main.cpp(8) : error C2784: 'class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> __cdecl std::operator +(_D,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce template a
rgument for '' from 'char [7]'
D:\Program Files\Microsoft Visual Studio\MyProjects\String\main.cpp(8) : error C2677: binary '+' : no global operator defined which takes type 'class String' (or there is no acceptable conversion)
Error executing cl.exe.
String.exe - 2 error(s), 0 warning(s)
I don't know what to do.
kedaman: Is overloading the 'equals' operator what you meant in your last post?
-
hmm i'm not sure why you are getting the first error, probably irrelevant.
Have you set a copy constructor to take in a string? The assignment operator (yeah it's "=") is not called at the initialization line, sorry I didn't notice.
-
This is the whole class declaration (if it helps):
Code:
class String
{
private:
char* theBuffer;
ulong theLen;
public:
String(); // default constructor
String(ulong newLen); // constructor receives length of string
String(char* newString); // constructor intializes with string
String(const String&); // copy contructor
~String(); // destructor
char & operator [](ulong) const; // String[x] - returns a reference to the char at offset x
const char* operator ()() const; // default operator - Same as GetString()
String& operator =(const String&); // String1 = String2
String& operator =(const char*); // String1 = "abcde"
String& operator =(char); // String2 = 'x'
String operator +(const char*); // String3 = String2 + "abcdef"
String operator +(char); // String3 = String2 + 'k'
String operator +(const String&); // String3 = String2 + String1
ulong GetLen() const { return theLen; } // returns the length of the string
char* GetString() const { return theBuffer; } // returns the string as a char pointer
};
-
BTW, I don't think it has anything to do with constructors:
Code:
int main()
{
String myStr1 = "World...";
String myStr2;
myStr2 = "Hello " + myStr1;
cout << myStr2() << endl;
return 0;
}
This gives me the same errors as in the above post. :(
-
okay, then there's something else going, on :(
-
global maybe
maybe you should discard all operator+ in your class and use a global addition operator instead
String operator+(String a, String b);
and let the constructors convert the operands to individual Strings
-
I want it to be encapsulated in the class.
Isn't there any way to get it working within the class?
-
The expression is beyond the class anyway, so it's a logical choise.
Hmm, I'll try to look into something anyway.
-
-
global operation seems to be a common solution to this polymorphic issue. Say you assign a datatype with String constructor, the following expression: char* + char*. How is the compiler suppose to know the expression has to be converted to String when there's no String type specified? The convertion to the type assigned is not explicit enough since any arguments passed in overloaded constructors can be implicitely converted, so there's a ambiguety issue. Therefore the compiler doesn't even try convert the whole expression to String. A global overloaded operator would convert char*+char*but on the other hand would suggest ambigueties with other global overloaded operators with both hand argument types containing a char* constructor. But thats a small chance and if that ever happens you can explicitely use type casting.
-
I guess I'll have to go with the global approach.
Thanks. :)