access to private member of same class type

P

psp

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.
 
V

Victor Bazarov

psp said:
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. Access is granted. Why is it surprizing to you? That's how
it has been for decades.

V
 
Z

Zachary Turner

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.

Imagine how silly it would be if every time you wanted to write an
equality operator or copy constructor you had to write something like
this:

class A
{
friend class A;
};
 
J

John Harrison

psp said:
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 this is surprsing to you then you have misunderstood the purpose
of private.

The purpose of data hiding (i.e. making things private) is to seperate
trusted code (i.e. all the code in class C, plus any friend code) from
untrusted code. This is useful in the real world.

The purpose is not to seperate objects from each other. That would make
data hiding harder, because every object would have to have public
accessors for everything, otherwise it would be impossible to copy or
compare objects.

john
 
M

Mikey

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.
 
V

Victor Bazarov

Mikey said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top