-2

Let's say I create a class object, with certain private variables and public accessor and mutator public functions within the class to access/change those variables.

Now let's say I create an instance of that class object, let's call it A, within the main functions. And in main(), I call functions declared outside of main(), to which I pass in A, the instance the class object. What I want to do is, in these outside functions, use the already implemented public mutator functions to, outside of main(), change the values in the existing private variables of instance A. With my code, I've been trying to do that, by let's say passing in like so:

randomFunction(objectInstance);

And within the random function defined outside of main(), I've tried changing the values within the private variable like so:

void randomFunction(classObject objectInstance){
...
objectInstance.changeValue(657428391);
...
}

but the actual value isn't updated in main(). Is there some pointer magic I can do with class object instances?

emmanuel
  • 9,607
  • 10
  • 25
  • 38
David W
  • 11
  • 2

4 Answers4

2

You pass the classObject by value, not by reference, so you are making a copy of the object instead of modifying the one you passed in.

So any changes you make to objectInstance are to the local copy of the object, not the passed in object. You'd have to change the function signature to

void randomFunction(classObject& objectInstance)

Or you could use a pointer

void randomFunction(classObject* objectInstance)

Then call the function as

randomFunction(&objectInstance);
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1

As far as I could understand you are passing the object be value and not by reference. Use:

void randomFunction(classObject &objectInstance){

In this way refernces will be copied and the changes that you make in the randomFunction will be reflected back in the main function.

Dhaval Kapil
  • 385
  • 3
  • 10
0

You are passing the object by value. When you pass an object by value, the function is working on a temporary copy of the object, not the actual object itself. If you do that, any changes you make in the function is not reflected back to the caller.

Pass the object by reference instead of by value:

void randomFunction(classObject& objectInstance)
{
    objectInstance.changeValue(123456);
}

You will now see the value reflected back to the caller, since you are actually using the object that was passed.

Please note that this is no different than if the type were an int, double, etc. Just because it is an object doesn't change the rules. For example:

#include <iostream>
void someFunction(int x)
{
   x = 10;
}

int main()
{
   int a = 0;
   someFunction(a);
   std::cout << a;
}

You will see that the value of a is still 0, even though a function was called that changed the passed-in variable to 10. The reason is again, the function someFunction takes its parameter by value. Change it to a reference, and you will see a change to 10.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
0

I suppose that your function can looks like this:

void DoSomeStuff(A instance);

If this is your case, you are creating a new variable that is a copy, so de facto you change local copy, not your actual variable.

You can declare it like

void DoSomeStuff(A &instance); //reference to A

or

void DoSomeStuff(A *instance); //pointer to A

In these cases, you will really change your variable you pass into these functions

Quest
  • 2,764
  • 1
  • 22
  • 44