black_gay
black_gay

Reputation: 143

Operator overloading on temporary object

im trying to perform operator overloading on a temporary object like in this example:

class changeStrToInt
{
public:
     int operator()(std::string x) {
         return 10;
     }
};


int main()
{
    changeStrToInt obj; //works
    int i = obj("lala");

    int x = changeStrToInt("lala"); // doesnt work
}

So my question is how can i make int x = changeStrToInt("lala"); work?

Upvotes: 0

Views: 306

Answers (1)

Jarod42
Jarod42

Reputation: 217583

changeStrToInt("lala") tries to call constructor of changeStrToInt with "lala".

You want:

  • changeStrToInt()("lala")
  • or changeStrToInt{}("lala")

Upvotes: 1

Related Questions