Hi, I'm new to C++ and OOP. I've been having a problem with a concept.
class samp
{
int i;
public:
samp(int n)
{
i = n;
}
int func(samp ob)
{
return ob.i;
}
};
Now, i is a private member of the class samp. So, how can the parameter ob access i directly? I know func(ob) is a member function of the class. But I thought private members can only be accessed with a this pointer.
Question
Gigi Buffon
Hi, I'm new to C++ and OOP. I've been having a problem with a concept.
Now, i is a private member of the class samp. So, how can the parameter ob access i directly? I know func(ob) is a member function of the class. But I thought private members can only be accessed with a this pointer.
Can anyone explain why the above code works?
Link to comment
https://www.neowin.net/forum/topic/1025404-c-accessing-private-member-variables/Share on other sites
10 answers to this question
Recommended Posts