E
E
Eugene Ordinary2016-12-12 15:40:14
Programming
Eugene Ordinary, 2016-12-12 15:40:14

How to define a class method so that the object is not passed to it by reference?

Let be

class MyClass 
{
  int x, y;
  int sum1();
}

MyClass::sum1() { return x+y; }

If I understand correctly, then
MyClass MyVar;
MyVar.sum1();

is equivalent to calling the function sum2
int sum2(MyClass &MyVar) { return MyVar.x + MyVar.y }
MyClass MyVar;
sum2(MyClass &MyVar);

And how can you define a class method so that the object is not passed to it by reference? To have something like this:
int sum2(MyClass MyVar) { return MyVar.x + MyVar.y }
MyClass MyVar;
sum2(MyClass MyVar);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2016-12-12
@Mercury13

UPD. Now I understand what you mean. It is not possible in this form.

S
sitev_ru, 2016-12-12
@sitev_ru

Some tricky question) Apparently so

//описание
static int MyClass::sum2(MyClass MyVar) { return MyVar.x + MyVar.y }

//вызов
MyClass::sum2(MyVar);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question