https://stackoverflow.com/a/8523361/11862989 from this answer this question is came into picture. I feel small thing is wrong in that answer, so I commented out there but no reply from him (guy (190K Reputation) who's answer I am referring) so I am asking here that small part.
1.
.h
struct A
{
private:
int &i;
public:
A(int);
};
.cpp
A::A(int k1): i(k1)
{
std::cout<<&k1<<" "<<k1<<"\n";
std::cout<<&i<<" "<<i<<"\n";
}
main.cpp
int main()
{
int x=99;
std::cout<<&x<<" "<<x<<"\n";
A a1(x);
}
Output
0x72fe0c 99
0x72fde8 99
0x72fde8 99
2.
.h
struct A
{
private:
int &i; // __change__
public:
A(int&);
};
.cpp
A::A(int &k1): i(k1) // __change__
{
std::cout<<&k1<<" "<<k1<<"\n";
std::cout<<&i<<" "<<i<<"\n";
}
main.cpp
int main()
{
int x=99;
std::cout<<&x<<" "<<x<<"\n";
A a1(x);
}
Output
0x72fe0c 99
0x72fe0c 99
0x72fe0c 99
in 2nd code as we are passing reference of x
through main and it is collected by k1
. and now we passing reference of k1
to i
. means now i
is referring to k1
and k1
is referring to x
. means indirectly i
is referring to x
am I right ?
in 1st I think here value of variable x
is passed through main and it is collected by variable k1
and then reference of k1
is passed to variable i
. so in this case variable i
is referring to variable k1
but variable k
is not referring to variable x
am I right ?
that guy(190K Reputation) whose answer I referred at top he used 1st method to do this, I think he is wrong and __2nd__method is right for initializing reference variable of object in class. am I right ?