-1

I am trying to create the operator that makes the same thing as constructor do. I have created the overloaded output operator << for this class , it was easy.

I just wanna type nameOfObject+(value) to create new instance of foo class.

I tried this:

foo& operator+(int x, foo& f) {
    f tmp = new foo(x);
    return tmp;
}

But I got error message that says I need to use ; after tmp;

#include <iostream>
#include <memory>

class foo {
    private: 
        int x;
    public: 
        foo(int x) { this->x = x; }
        int getX() { return this->x; }        
};

std::ostream& operator<< (std::ostream& text, foo& f) {
    text  << f.getX();
    return text;
}

int main()
{
    foo bar(2);
    std::cout <<bar; //returns 2

    return 0;
}

UPDATE_1:
For example, I have the heightOfTheTree variable in my class. Using foo tree1(5) - normal constructor I just want to assign the 5 to my variable. But using foo tree2+5, I want to create new object with value multiplied twice(for example).

  • 2
    Use of `f tmp = new foo(x);` indicates to me that you are not well versed with the basics of the language. It will be good for you to read through [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – R Sahu Jan 26 '18 at 06:38
  • What's the name of the type, and what's the name of the parameter? – molbdnilo Jan 26 '18 at 06:46
  • This similar question might be of interest: [how to add three objects of same class in c++?](https://stackoverflow.com/a/46903810/7478597) (which was btw. marked as duplicate) where I presented multiple solutions for `operator+` overloading. – Scheff's Cat Jan 26 '18 at 06:47
  • your question suggests that you want the expression ```bar + n``` to return a copy of ```bar```, but don't you really want it to return a copy of bar who's value is ```bar.getX() + n```? – Richard Hodges Jan 26 '18 at 07:04

2 Answers2

0

You're almost there.

It should probably look something like this. Comments inline:

#include <iostream>
#include <memory>

class foo {
    private: 
        int x;
    public: 
        foo(int x) : x(x) { }

        // add a const modifier because this function does not modify foo
        int getX() const { return this->x; }        

        // it usually makes sense to implement + in terms of +=
        foo& operator+=(int arg)
        {
            x += arg;
            return *this;
        }
};

// implement + in terms of += on a copy
foo operator+(foo l, int r)
{
    l += r;
    return l; 
}

// for this class, make addition commutative. i.e x+y == y+x
foo operator+(int l, foo const& r)
{
    return r + l;
}

// add a const modifier because we expect and demand that this operation
// will not modify f
std::ostream& operator<< (std::ostream& text, foo const& f) {
    text  << f.getX();
    return text;
}

int main()
{
    foo bar(2);
    std::cout <<bar; //returns 2

    foo bar2 = bar + 5;
    std::cout <<bar2; //returns 7

    foo bar3 = 5 + bar2;
    std::cout <<bar3; //returns 12

    return 0;
}

Note that we don't use new to copy objects. Java and C# require that. In c++ we generally prefer to treat objects as values.

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
0

This will create a new foo object.

foo operator+(foo& f, int x) {
    return foo(f.getX() + x);
}

//Usage
foo bar(2);
foo test = bar + 5;
std::cout << test; //outputs 7
super
  • 12,335
  • 2
  • 19
  • 29
  • It look nice, but how to make it like `foo test+5`. Your code is creating new object using another. How to create new object without using another? Is it possible? – sqlMasterSOon Jan 26 '18 at 15:28
  • @sqlMasterSOon Not sure I understand. What would be the difference between `foo test+5;` and `foo test(5)` ? – super Jan 26 '18 at 15:45
  • Because For example I have the `heightOfTheTree` variable in my class. Using `foo tree1(5)` - normal constructor I just want to assign the `5` to my variable. But using `foo tree2+5`, I want to create new object with value multiplied twice(for example). – sqlMasterSOon Jan 26 '18 at 16:00
  • @sqlMasterSOon Can you edit your question and put an actual usage example in code there. – super Jan 26 '18 at 16:04