0

These were given as examples of incorrect usage of operator overloading, but I am not certain as to why that is. This is in terms of C ++, which I am not too familiar with either. Could anyone explain why these wouldn't work if they were used? I know they are not complete codes, but I think the formatting was the issue being highlighted here.

(1)

class Mystery                    //Line 1
{
  ...
  bool operator <= (Mystery);     //Line 2
  ...
};

bool Mystery::<=(Mystery rightObj)    //Line 3
{
  ...
}

(2)

class Mystery                       //Line 1
{
    ...
    bool operator <= (Mystery, Mystery);  //Line 2
    ...
};

(3)

class Mystery                       //Line 1
{
    ...
    friend operator+ (Mystery);         //Line 2
    //Overload the binary operator +
    ...
};
prajmus
  • 3,171
  • 3
  • 31
  • 41

3 Answers3

1

I'll try to be a little more thorough than the answers so far:

(1)

bool operator <= (Mystery);     //Line 2
  1. Const correctness for the invocant. Operator <= is not supposed to modify the instance it is called on and is supposed to indicate not doing so by the trailing const modifier:

    bool operator <= (Mystery) const;
    
  2. Everybody else suggested that the right argument should be passed by const reference, but here it depends on what the class contains. Numbers are normally passed by value and if the Mystery contains just one member of primitive type and has default or inline copy constructor, passing by value might be appropriate. When passing by value, the argument obviously can't be modified, so you don't need to care for const. Applies to all further cases too. Using const reference

    bool operator <= (Mystery const &) const;
    

    is more common because most user classes do contain more than just one or two numbers and for the trivial cases everything is usually inline anyway and the compiler will optimize it as it sees fit.

  3. If the class has any constructors suitable for type conversion (non-explicit single argument constructors from different types), the operator should be overloaded as non-member function (like the operator+ in the case (3)).

    This is because a non-member operator<= will be found if the arguments are merely convertible to Mystery, while member operator will only be found if the left operator is Mystery or derived type.

    Overloaded operators are only considered if at least one argument is a user-defined type, i.e. the compiler will not consider bool operator<=(Mystery, Mystery) on arguments of type (int, int) even when there is a Mystery::Mystery(int) constructor that would make it viable conversion. It will however consider non-member operator for argument list of (int, Mystery) while it will not consider a member bool Mystery::operator<<(Mystery) const operator for that.

bool Mystery::<=(Mystery rightObj)    //Line 3

Missing operator keyword.

bool Mystery::operator<=(Mystery rightObj)

(2)

bool operator <= (Mystery, Mystery);  //Line 2

Incorrect signature. operator<= takes 2 arguments, one of them is the invocant and it therefore needs just one explicit argument.

(3)

friend operator+ (Mystery);         //Line 2

This refers to a non-member operator+.

  1. Since operator+ is also binary operator, it needs two arguments.

  2. Operator + needs a return type. The return type should usually be a new instance (i.e. return by value) of the argument class.

Non-member functions don't have the trailing const modifier, because they don't have invocant. The arguments should be passed either by value or by const reference depending on the content of the class:

friend Mystery operator+ (Mystery, Mystery);
friend Mystery operator+ (Mystery const &, Mystery const &);

The friend keyword only appears when declared inside the class to give the function access to it's private members. Outside the class (e.g. in definition) it is just:

Mystery operator+ (Mystery, Mystery);
Mystery operator+ (Mystery const &, Mystery const &);
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
0

Case (1) Error: Expected an idenifier operator

You are missing the Keyword operator on Line 3

bool Mystery::operator<=(const Mystery& rightObj)    //Line 3
{
  ...
}

Case (2) Error: Too many parameters for a relational operator

A relational operator as a member function overload should have only one parameter

bool operator <= (const Mystery&);  //Line 2

Case (3)

Explicit return type is missing

friend Mystery operator+ (const Mystery&);         //Line 2

Other nitpicks

  1. If a function is not supposed to mutate an argument, make it a const reference.
Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • Well, function taking a value (as in the question) can't modify it's argument either. Or rather it can internally modify it's copy whatever it likes, but it does not affect the value in the caller. – Jan Hudec Feb 03 '14 at 08:40
  • The invocant is always passed by reference though, so const correctness is required for it. – Jan Hudec Feb 03 '14 at 08:41
  • @JanHudec: I am not sure if I got your comments. Are you indicating that my last sentence is wrong? I believe I had said the same thing as your comments state. – Abhijit Feb 03 '14 at 08:42
  • Yes, I am indicating that sentence is wrong, or rather critically incomplete. – Jan Hudec Feb 03 '14 at 08:43
0

bool operator <= (Mystery);
right side object should be a const reference.

bool Mystery::<=(Mystery rightObj)
Wrong syntax, should be:
bool Mystery::operator <=(const Mystery& rightObj)

bool operator <= (Mystery, Mystery); Only one parameter is used by operator - error

friend operator+ (Mystery); Trying to make a global operator+ a friend, should use global scope "::". Global operators need 2 inputs, not one. Use const reference. Operator should also return a reference to "this".

Mystery& operator+(const Mystery& lhs, const Mystery& rhs);

egur
  • 7,830
  • 2
  • 27
  • 47
  • Don't forget about the _left_ side object! – Jan Hudec Feb 03 '14 at 08:42
  • I don't see that fixed. The `operator<=` still takes left argument via non-const reference. And the other part of your edit is actively wrong. That is `operator+`, **not +=**. It must return a new value and that can only be done by value! Not to mention that non-member operators don't have `this`. – Jan Hudec Feb 03 '14 at 12:57