4

I have a method that takes an object as an argument.

Both the caller and argument have the same members (they are instances of the same class).

In the method, particular members are compared and then, based on this comparison, one member of the argument object needs to be manipulated :

class Object {

   // members

public: 

someMethod (object other) {

   int result;
   member1 - other.member1 = result;
   other.member2 = other.member2 - result;

}

The only thing is that it doesn't actually change other.member2 out of this scope, and the change needs to be permanent.

So yes, sorry: I need advice on pointers... I have looked online and in books, but I can't get it working. I figure one of you will look at this and know the answer in about 30 seconds. I have RFTM and am at a stupid loss. I am not encouraging.

Thanks everyone!

  • 3
    Careful: C++ is type sensitive. I assume that the difference between `class Object` and `object other` was introduced when you retyped it for the question, but you might want to make sure the case is consistent in your actual code. – Ben Voigt Jul 25 '10 at 20:04
  • 2
    Also, you can't use subtraction on the left hand side of an assignment. Should that be `result = member1 - other.member1;` ? – Ben Voigt Jul 25 '10 at 20:05
  • Yes, both your suggestions have been taken into account in actual code! Thanks. – Alec Sloman Jul 25 '10 at 20:12
  • (You should properly @address users when you reply in comments, otherwise they won't see your reply in their comment responses list.) You might want to look at [How to pass objects to function is C++?](http://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c/2139254#2139254) – sbi Jul 25 '10 at 20:47
  • @sbi Thanks for that! I will have a look at that discussion. – Alec Sloman Jul 25 '10 at 23:00

2 Answers2

8

This is because you are passing by value (which equates to passing a copy. Think of it as making somebody a photocopy of a document and then asking them to make changes, you still have the original so the changes they make won't be reflected in your copy when you get it back. But, if you tell them where your copy is located, they can go and make changes to it that you will see the next time you go to access it). You need to pass either a reference to the object with

Object& object

or a pointer to the object

Object * object

Check out this page for a discussion of the differences.

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
4

You are passing a copy of other to the someMethod function. Try passing a reference instead:

someMethod(object &other) { ...

When you pass a copy, your change to other.member2 changes only the copy and not the original object. Passing a reference, however, makes the other parameter refer to the original object passed in to the call to someMethod(obj).

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 2
    As a side note: passing complex objects as copies is expensive. Therefore even if the object is not changed inside the method, it is often preferable, to pass it as const reference. This avoids the copy and gives the caller a guarantee that the object isn't changed. – ollb Jul 25 '10 at 20:06