Code:
#include <iostream>
using namespace std ;
class N
{
private:
int num ;
public:
int &geta(int n)
{
num = n ;
return num ;
}
int get() { return num;}
};
int main()
{
N n ;
int &a = n.geta(12) ;
cout <<"First go: " << n.get() <<endl ;
a = 100;
cout << "Second go: " << n.get() ;
n.geta(1)=19 ;
cout << "Third go: " << n.get() ;
return 0 ;
}
Code:
int &a = n.geta(12) ;
i understand this piece of code. n.geta() returns a reference of member variable of object n to a
so a <b>IS</b> num, an alias, but still num - and can be accessed like a normal variable which breaks the rule of encapsulation and access specifiers.
as i have done with this code
but I don't get this!!
Code:
n.geta(1)=19
number 1 where i am not storing the reference of num in any reference variable.
i was reading some stuff that n.geta() because it returns a reference can be used as a lvalue..
but i don't get it!
where does the value that n.geta(1) go to?
why is it that 1 which should be stored in num is overwritten by 19..or is it overwritten?
Speaking of that
how is it possible to initialize a value to a function as if it was a variable?
i keep trying to evaluate it and each time i fail:
geta(1) takes a parameter which is stored in num, so num is 1
it returns the value 1..but there is no reference variable in main to hold it..
so it is floating somewhere in memory...and I just can't go on..
what is 19? initialized to ?
to num but how is that possible? 19 is not an argument, also .geta(1) is returning not storing...ack! can someone help!! THanks
oh my God can someone tell me what is happening on this code?