1

I'm fairly familiar with operator overloading, however I am wondering how do we implement something like this:

myClass myclassobj;
int x;
x = 5;
x = x + myclassobj

There is no way to overload the + operator for the int class, so something should be done from myClass, but how would we do that? I am probably using the wrong keywords, but searching through SO didn't lead to anithing. Apologies if I did something wrong, this is my first post here.

Edit - My class is a custom vector class, so simply converting it to given type won't work.

2 Answers2

5

Define an overloaded operator with the signature int operator+(int, myClass).

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • Thank you for the swift answer. Do I define this operator inside myClass? Because when I try to do that, I get an error saying that the operator takes either 0 or 1 argument. Perhaps I have to define it outside ? – DefinitelyNotMe Nov 29 '15 at 01:21
  • @DefinitelyNotMe Please see this thread: http://stackoverflow.com/questions/4421706/operator-overloading – Brian Bi Nov 29 '15 at 01:25
  • Thank you! That will surely come in handy. I will read the parts that interest me tomorrow morning then. – DefinitelyNotMe Nov 29 '15 at 01:32
1

You are right.

There is no way to overload the + operator for the int class, so something should be done from myClass

Your question:

but how would we do that?

My answer:

You should use user defined type conversion. It may work with a conversion operator.

#include <iostream>

class myClass
{
    int i;
public:
    myClass(int i=0) : i(i) {  }
    operator int(){ // A conversion from myClass to int may solve your problem.
        return i;
    }
};

int main()
{
    myClass myclassobj(99);
    int x=7;
    x = 5;
    x = x + myclassobj;

    std::cout<<x<<std::endl;

    return 0;
}

Brian gave a good answer also, but it works only if the overloaded operator does not need protected or private members from the second argument or if overloaded operator declared as friend of myClass.

Dániel Sándor
  • 1,246
  • 8
  • 14
  • Hello! I see how that would work! Unfortunately, in my case it's a class representing a vector, and my operators perform the given operation on every element of the vector, so converting it to an integer is impossible. – DefinitelyNotMe Nov 29 '15 at 01:27
  • 1. If you would like to modify myclassobj with the operator+, then the second parameter should be passed by non-const reference not by value. 2. If that vector is private or protected in MyClass, then the global operator+ should be declared friend of MyClass. There is a code below that may help you and represents what I meant. – Dániel Sándor Nov 29 '15 at 02:05
  • #include #include class myClass { std::vector ints; // It is myClass' secret. public: myClass(){ } friend int operator+(int, myClass&); // operator+(int, myClass&) is a friend of myClass, so he knows her secrets. }; int operator+(int i, myClass& mc){ // mc is passed by non-const reference, so it can be modified in the function body. for(int i=0; i – Dániel Sándor Nov 29 '15 at 02:05
  • Oh that works wonders! Thank you exactly what I was looking for. – DefinitelyNotMe Nov 29 '15 at 11:52