private datamember access control

J

john sun

Hi,

I don't understand one thing when authoring cp ctor. The below is simplified
class and cp constructor.
When initializing the data member, we use object ref TEST to access its
private member m_x. Why in this case it's allowed even m_x is private member
of object TEST?

Thanks very much!

John

class test
{
private:
int m_x;
public:
test(const test& TEST):m_x(TEST.m_x)
{

}
}
 
D

David Harmon

When initializing the data member, we use object ref TEST to access its
private member m_x. Why in this case it's allowed even m_x is private member
of object TEST?

You are the author of class test; you are trusted to not violate the
requirements for accessing class test members. Access control is by
class, not by instance.
 
J

john sun

Very good point.
Thanks!
David Harmon said:
You are the author of class test; you are trusted to not violate the
requirements for accessing class test members. Access control is by
class, not by instance.
 
M

Martijn Lievaart

Hi,

I don't understand one thing when authoring cp ctor. The below is simplified
class and cp constructor.
When initializing the data member, we use object ref TEST to access its
private member m_x. Why in this case it's allowed even m_x is private member
of object TEST?

Members are *class* private, not *object* private. One object can touch
the internals of other objects of the same class. The reason is mainly so
code like yours is allowed. If it was not allowed, copy constructors and
assignment operators would be a pain to write.

HTH,
M4
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top