samedi 26 mars 2016

What is the difference between not implementing and deleting common operators?

Let's say I have a class

class Object
{
public:
    Object(int i) : num(i) {};

    int getNum() const { return num; }
private:
    int num;
};

Now, if I try to do this

Object obj{ 1 };
Object obj2{ 2 };
Object obj3 = obj + obj2; //This is wrong

This is illegal: 'Object' does not define this operator or a conversion to a type acceptable to the predefined operator.

Adding Object operator+(const Object&) = delete; doesn't really change anything, except for the error message: 'Object Object::operator +(const Object &)': attempting to reference a deleted function.

Is the delete only needed for operators that have an implicit declaration (like assignment operator and copy/move constructor) or does it change anything else in my case?

Aucun commentaire:

Enregistrer un commentaire