Mikey wrote:
On Jun 21, 8:44 pm, psp <pradhan.push...@gmail.comwrote:
>I'm really puzzled why this is allowed by the c++ compiler:
class C {
public:
void setNumber( int y ) { m_y = y; }
void printNumber() const { cout << "m_y= " << m_y << endl; }
void copyInt(C &anotherCObj) { m_y = anotherCObj.m_y; }
private:
int m_y;
};
Is there a c++ rule/specification that allows access to private
members of a class if accessed from the same class.
Yes. If class members and methods were completely private to
everything, there would be no point to it. There would be no way to
access the private data or functions, making that dead code.
That's not entirely true. The private members could have been made
to be only accessible through 'this->'. It would certainly make more
secure execution envinonment, but the use would be rather limited.
There were several discussions in the past years about a specifier
like "very_private" which would restrict access to only the current
instance. My guess is that due to its limited usefulness it wasn't
made part of the language.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask