Reputation: 395
In this code why can I access the private member of the object with no compiler error?
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents=0)
{
m_nCents = nCents;
}
// Copy constructor
Cents(const Cents &cSource)
{
m_nCents = cSource.m_nCents;
}
Cents& operator= (const Cents &cSource);
};
Cents& Cents::operator= (const Cents &cSource)
{
cSource.m_nCents is private why can I do the following:
m_nCents = cSource.m_nCents;
// return the existing object
return *this;
}
Upvotes: 5
Views: 2939
Reputation: 361812
You can access private members from member-functions/constructors/destructor/freinds of the class. It is class-based accessibility, not object-based accessibility.
Upvotes: 4
Reputation: 272832
Because private
means "visible accessible to the class", not "visible accessible to the object".
Upvotes: 9