Operator Overloading

Programming, for all ages and all languages.
Post Reply
Guest

Operator Overloading

Post 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.
Tim

Re:Operator Overloading

Post by Tim »

Your operator= should look like this:

Code: Select all

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

Re:Operator Overloading

Post 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!";
ark

Re:Operator Overloading

Post 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);
};
mystran

Re:Operator Overloading

Post 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..
Guest

Re:Operator Overloading

Post by Guest »

Hi again,

Thanks everyone, returning a reference solves the problem.
Joel (not logged in)

Re:Operator Overloading

Post 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.
mystran

Re:Operator Overloading

Post 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.
ark

Re:Operator Overloading

Post by ark »

I'll take your word for it, because that really sounds more correct than what I suggested might be the case.
Post Reply