code
#include<iostream>
struct A
{
private:
public:
int &p,q;
A(int &k1,int k2):p(k1),q(k2)
{
}
};
int main()
{
int x=2;
A a1(x,3);
std::cout<<&x<<"\n";
// std::cout<<&k1<<"\n"; commented out this as it gives error
std::cout<<&a1.p<<"\n";
}
Output
0x6ffe1c
0x6ffe1c
p
is referring to k1
and k1
is referring to x
.
K1
is going to out of scope right so trying to access it gives error okay. But a1.p
actually referring to k1
so it is referring to memory which is not exist. so why accessing a1.p
not gives error.