By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,367 Members | 946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,367 IT Pros & Developers. It's quick & easy.

private member access

P: 9
Hi,

i am just now studing c++ and came across somthing that seemed strange:

i've ran this code:
class ClassA
{
private:
int _value;
public:
ClassA(const int value) : _value(value){ }

// can access private members of val when in the ClassA implementation???
ClassA(const ClassA& val) : _value(val._value){ }
};

void main()
{
ClassA test1(10);
}

i don't understand... doesn't all that private means that no one is allowed to change the members of ANY outside ClassA val.
is this just a problem with the compiler alowing this?
Aug 8 '07 #1
Share this Question
Share on Google+
1 Reply


RRick
Expert 100+
P: 463
Private is concept between different classes. Private does not restrict access for instances/objects of the same class.

Your second constructor (the copy constructor) is a good example of this. In this case the "this" object can access the private parts of the "passed" object. If this wasn't possible, we wouldn't be able to copy any objects.
Aug 9 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.