S
S
sddvxd2018-03-23 21:18:40
C++ / C#
sddvxd, 2018-03-23 21:18:40

Why does this function return an object (operator overloading)?

Good evening

Factory Factory::operator*(const int& a)const{
  return a*this->a;
}

works well for this example
Factory obj1 = 1; //Первый  единственный параметр для конструктора - int 
Factory obj2 = 2 * obj1;

Question: in return I multiply two ints, the function returns a copy of the Factory, why is that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2018-03-23
@sddvxd

Question: in return I multiply two ints, the function returns a copy of the Factory, why is that?

Because that's how you defined the operator. I would write int Factory::operator *- everything would be different.
If your question is "why does it compile", then the answer is because Factory has a non-explicit constructor that can be called with an int parameter. This constructor creates the object returned from the * operator.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question