Page 1 of 1

Operator Overloading

Posted: Mon Aug 04, 2003 8:54 am
by Guest
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.

Re:Operator Overloading

Posted: Mon Aug 04, 2003 10:01 am
by Tim
Your operator= should look like this:

Code: Select all

string& string::operator=(const char *str)
{
    // assign string
    return *this;
}

Re:Operator Overloading

Posted: Mon Aug 04, 2003 10:04 am
by Schol-R-LEA
IIRC, overloaded assigment operators cannot be used as constructors; you'll need to use

Code: Select all

string s1;
s1 = "A String!";
string s2(s1); 
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, s2;
s2 = s1 = "A String!";

Re:Operator Overloading

Posted: Mon Aug 04, 2003 5:55 pm
by ark
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:

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

Posted: Tue Aug 05, 2003 12:02 am
by mystran
Like others have pointed out, you need to have your operator= return the object (or a reference to it for better performance).

Code: Select all

s1 = s2 = "a string";
is basicly parsed as

Code: Select all

s1.operator= (s2.operator= ("a string"));
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..

Re:Operator Overloading

Posted: Tue Aug 05, 2003 4:55 am
by Guest
Hi again,

Thanks everyone, returning a reference solves the problem.

Re:Operator Overloading

Posted: Tue Aug 05, 2003 8:40 am
by Joel (not logged in)
The point about constructors being that although

Code: Select all

s1 = s2 = "a string";
is parsed as:

Code: Select all

s1.operator=(s2.operator=("a string"));
The following:

Code: Select all

string s1 = s2 = "a string";
is parsed as:

Code: Select all

s1.constructor(s2.operator=("a string"));
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.

Re:Operator Overloading

Posted: Tue Aug 05, 2003 10:11 am
by mystran
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.
Nope.
It will always be parsed as:

Code: Select all

s1.constructor(s2.operator=("blaah"));
If no copy-constructor is defined, the default is used, which is a binary copy of the object. If you declare the copy-constructor private, you can't do that at all.

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

Posted: Tue Aug 05, 2003 3:57 pm
by ark
I'll take your word for it, because that really sounds more correct than what I suggested might be the case.