在此代码中,为什么我可以访问对象的私有成员而没有编译器错误?
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)
{
Run Code Online (Sandbox Code Playgroud)
cSource.m_nCents是私有的,为什么我可以执行以下操作:
m_nCents = cSource.m_nCents;
// return the existing object
return *this;
}
Run Code Online (Sandbox Code Playgroud)
因为private" 可见类可见 ",而不是" 对象可见 ".
| 归档时间: |
|
| 查看次数: |
2867 次 |
| 最近记录: |