Operator Overloading
Operator Overloading
Hi,
I have created a basic string class and have overloaded operator= and everything works fine, however I cannot do things like this
string s1;
string s2 = s1 = "A String!";
I get an error "cannot convert `void` to `string`"
I am using Bcc55 and just wondering if this is even possible and if you guys have any ideas on how to do it.
thanks.
I have created a basic string class and have overloaded operator= and everything works fine, however I cannot do things like this
string s1;
string s2 = s1 = "A String!";
I get an error "cannot convert `void` to `string`"
I am using Bcc55 and just wondering if this is even possible and if you guys have any ideas on how to do it.
thanks.
Re:Operator Overloading
Your operator= should look like this:
Code: Select all
string& string::operator=(const char *str)
{
// assign string
return *this;
}
Re:Operator Overloading
IIRC, overloaded assigment operators cannot be used as constructors; you'll need to use
or the equivalent. I would have to check to be sure I have that correct, however.
Aside from that, I'd check to be sure that it works if you separate the assignment from the declaration expressions, i.e.,
Code: Select all
string s1;
s1 = "A String!";
string s2(s1);
Aside from that, I'd check to be sure that it works if you separate the assignment from the declaration expressions, i.e.,
Code: Select all
string s1, s2;
s2 = s1 = "A String!";
Re:Operator Overloading
The assignment operator in the context of an object declaration should invoke a constructor, not an operator=, so make sure you have appropriate constructors defined, as well.
Your class should have at least the following:
Your class should have at least the following:
Code: Select all
class String
{
public:
String();
String(const char* pszSrc);
String(const String& strSrc);
const String& operator=(const char* pszSrc);
const String& operator=(const String& strSrc);
};
Re:Operator Overloading
Like others have pointed out, you need to have your operator= return the object (or a reference to it for better performance).
is basicly parsed as
which is two "normal" method calls.
So your compiler is just complaining that the argument of s1.operator=, that is the return value of s2.operator=, is void.
You might want the copy-constructor too, but that's not really necessary here..
Code: Select all
s1 = s2 = "a string";
Code: Select all
s1.operator= (s2.operator= ("a string"));
So your compiler is just complaining that the argument of s1.operator=, that is the return value of s2.operator=, is void.
You might want the copy-constructor too, but that's not really necessary here..
Re:Operator Overloading
Hi again,
Thanks everyone, returning a reference solves the problem.
Thanks everyone, returning a reference solves the problem.
Re:Operator Overloading
The point about constructors being that although
is parsed as:
The following:
is parsed as:
if the copy constructor is defined. If not, it would probably still be able to evaluate to two operator= calls, but I haven't checked that out to make sure.
Code: Select all
s1 = s2 = "a string";
Code: Select all
s1.operator=(s2.operator=("a string"));
Code: Select all
string s1 = s2 = "a string";
Code: Select all
s1.constructor(s2.operator=("a string"));
Re:Operator Overloading
Nope.Joel (not logged in) wrote: if the copy constructor is defined. If not, it would probably still be able to evaluate to two operator= calls, but I haven't checked that out to make sure.
It will always be parsed as:
Code: Select all
s1.constructor(s2.operator=("blaah"));
If you want, you can verify that operator= is NOT called by printing something from there. Same thing for the default constructor.
Re:Operator Overloading
I'll take your word for it, because that really sounds more correct than what I suggested might be the case.