Reputation: 851
I am a newbie in C++. Here in my case I have created two classes, class One
and class Two
. _x
is declared as a private member variable of class One
. So here I am trying to learn different methods to access this private variable(_x
) from any other class(Here in this case it is class Two
). This is my scenario basically. So one method I tried is ,making class Two
as a friend class
of class One
and for accessing this variable, I have used directValOfOne
function to print the value of this variable and another one I tried was passing the address of the class One
instance in another member function (getDataTwo
) of class Two
. Everything works well until here.
As you can see, I have initialized the _x
using a parameterized constructor and printing this variable gives me the value as 10
using both the methods. Again I modified this variable using a
setter function (setDataOne
) to 220
and when I print it using the getDataTwo
function it prints the value as 220
. But when I tried to print the value using directValOfOne
, it still showing the old initialized value (10
) which I am really confused. I have printed the address of class One
object in this directValOfOne
funtion and it shows same. If so then why the new value(220
) is not updated here.
Source code is placed below.
#include <iostream>
using namespace std;
class One{
int _x;
public:
One(int a):_x{a} {}
//setters and getters
void setDataOne(int a) { _x = a; }
int getDataOne() const { return _x; }
//print values
void printOne() { cout<<"_x - "<<_x<<endl; }
friend class Two;
};
class Two{
One _a;
int id_one = 0;
public:
Two(One a):_a{a} {}
//setters and getters
void setDataTwo(int a) { }
int getDataTwo(One *obj) {
id_one = obj->getDataOne();
return id_one;
}
void printTwo() { printf("id_one = %d\n",id_one); }
void directValOfOne() {
printf("address = %p\n",&_a);
printf("_a._x = %d\n",_a._x);
}
};
int main(){
One one(10);
Two two(one);
one.printOne();
two.getDataTwo(&one);
two.printTwo();
two.directValOfOne();
printf(" *********** \n");
one.setDataOne(222);
one.printOne();
two.getDataTwo(&one);
two.printTwo();
two.directValOfOne();
return 0;
}
and the console output is,
_x - 10 id_one = 10 address = 006dfee4 _a._x = 10 *********** _x - 222 id_one = 222 address = 006dfee4 _a._x = 10
As you can see the _a._x
still prints the value 10
. But actually the _x
is updated to new value 220
. Sorry if I explained it so detail. Just to give you an overview I detailed like this.
So my que is ,
Why it is not showing the updated value since the addresses are same?
Am I missing any important concept here ? If so please guide me to understand this problem.
Upvotes: 1
Views: 955
Reputation: 504
The problem in your code is that you pass to Two
an instance of class One
by value. This means that Two
stores a copy of the instance of One
you pass to it. When you change the value of _x
in one
it does not affect the value of _x
in _a
(inside two
).
In order for your code to work you need to pass a reference to One
in Two
's constructor and store this reference. Now every change to one
will also affect _a
.
Modified code:
class Two{
One& _a;
int id_one = 0;
public:
Two(One& a):_a(a) {}
// ...
Regarding the address issue, the address of _a
will stay the same since there's no reason for it to change once initialized. If you'll print the address of one
in your current code (before the suggested modifications) it should be in a different address than _a
(since _a
is a copy that's located somewhere else).
In the modified version of the code you should see the same address since it's the same object.
Upvotes: 3