简体   繁体   中英

operator not resolving to operator function c++

I have been doing some work with classes that need to have some of their operators overloaded (=, ==, +, !=, etc) I write the operator functions, but sometimes they are not being called, or the compiler is acting like it does not even exist. for example:

class Thing{
public:
    Thing& operator=(const Thing& _other){
        // implementation 
    }
    bool operator==(const Thing& _other){
        // implementation
    }
    Thing& operator+(const Thing& _other){
        // implementation 
    }
};

this is including other member functions of the class (constructor(default), constructor(copy), destructor, etc), but some times the method is ignored.

void Foo(Thing & thing1, Thing & thing2){
    //snip
    if(thing1 == thing2){
        //do some stuff
    }
    //snip
    Thing tempThing();
    thing1 = thing2 + &tempThing;
    //snip
}

in Visual-Studio I go to compile, and it trows that there is no operator that takes left side argument of Thing, and then if I specify thing2->operator+(&tempThing); then it works this is confusing because it should resolve to that operator function just by virtue of language resolution

if there exists a Class operator#(Class) function, and the arguments can be cast to the appropriate types then all that is need is to use the operator and the compiler will replace it with the operator function, or at the very least call the operator function.

thing1 = thing2 + thing3;   // written line 
thing1.operator=(thing2.operator+(thing3));

why would I have to specify the operator function. shouldn't the compiler do that for me.

EDIT: this is a question about general behavior as opposed to actual correctness of code. even when I write statements as they should be for some reason have to specify the actual operator() instead of being able to just use the symbol. (I have had to do this with direct examples as well copied character for character, but sometimes it does work)

Thing tempThing();

This isn't what you probably think it is. Google for "Most vexing parse".

thing1 = thing2 + &tempThing;

Since your operator+ takes references (not pointers) you don't want to take the address. Once you fix the preceding definition, it should compile fine as thing1 = thing2 + tempThing; .

Generally speaking, however, you want to avoid using member functions to overload most operators. The problem is that doing so allows conversion of the right operand to the correct type, but not of the left operand. By using a global overload instead, you get symmetry -- either operand can be converted.

It's common style to have operator+ , operator- , etc ..., out of your class or as a friend functions of your class and operator+= , operator-= , ..., as a member functions. This is because the later ones are modifying the object itself and the first ones are working on two objects and returning the third one. Here's a sample code for string class which could look like:

class string {
public:
        string& operator+=(const string& other)
        {
                this->append(other);
                return *this;
        }
private:
        void append(const string& other)
        {
                // concatenate two strings here
        }
};

string operator+(const string& first, const string& second)
{
        string result(first);
        result += second;
        return result;
}

For your case changing the lines

Thing tempThing();  
thing1 = thing2 + &tempThing;

to

Thing tempThing;  
thing1 = thing2 + tempThing;

should also work.

you are adding pointer of a function

Thing tempThing();
thing1 = thing2 + &tempThing;

Thing tempThing(); declares a function that returns Thing, Thing tempThing; creates Thing

also:

operator==, operator+

should probably be const

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM