Private member accessible from another object?

S

Siam

Hi all,

I read somewhere that private members of some instance of class A are
accessible from another instance of the same class. In other words,
access to private members is done 'on a class by class basis, not on an
instance by instance basis'. I couldn't quite believe it, and tried it
out for myself, and was shocked to see it was true... What is the
reasoning behind this? Why should one Person object be able to see
another Person object's private parts?

Cheers,
Siam
 
V

Victor Bazarov

Siam said:
I read somewhere that private members of some instance of class A are
accessible from another instance of the same class.

That's correct.
In other words,
access to private members is done 'on a class by class basis, not on
an instance by instance basis'.
Right.

I couldn't quite believe it,
Why?

and
tried it out for myself, and was shocked to see it was true... What
is the reasoning behind this?

It's done for simplicity, performance. Get a copy of D&E and read it.
Why should one Person object be able to
see another Person object's private parts?

I think you're assigning too much weight to real-world analogy here.
Access specifiers in C++ exist for different reasons than privacy laws
and moral foundations in the real world.

V
 
S

Siam

I couldn't quite believe it,

I dont see why encapsulation of private data shouldnt hold between
objects of the same class. They are objects in their own right, and
their members have been made private for a good reason.
I think you're assigning too much weight to real-world analogy here.
Access specifiers in C++ exist for different reasons than privacy laws
and moral foundations in the real world.

Hehe maybe, but I'm sure I can think up plenty of real examples where
it's downright dangerous to allow other objects (albeit of the same
type) accessing your private members.

Does this rule also apply extend to inherited classes? Can an object of
a derived class access private members of an object of the base class,
and vice versa? (My guess would be yes and no, respectively...)

Siam
 
V

Victor Bazarov

Siam said:
I dont see why encapsulation of private data shouldnt hold between
objects of the same class. They are objects in their own right, and
their members have been made private for a good reason.

Well, vast majority of C++ programmers don't think so or don't care.
Access specifiers don't exist for security, they exist to help create
software that works well.
Hehe maybe, but I'm sure I can think up plenty of real examples where
it's downright dangerous to allow other objects (albeit of the same
type) accessing your private members.

OK, you're on!
Does this rule also apply extend to inherited classes? Can an object
of a derived class access private members of an object of the base
class, and vice versa? (My guess would be yes and no, respectively...)

Please refer to your C++ textbook for the answer.

V
 
T

Thomas J. Gritzan

Siam said:
I dont see why encapsulation of private data shouldnt hold between
objects of the same class. They are objects in their own right, and
their members have been made private for a good reason.

Think about your class person and it's copy constructor.
How would it copy the private parts of the class? Should you declare
the class person as its own friend?
 
V

Victor Bazarov

Thomas said:
Think about your class person and it's copy constructor.
How would it copy the private parts of the class? Should you declare
the class person as its own friend?

Well, no, but you're trying to explain the [access] mechanism in its own
terms ["declare as friend"]. What the OP would rather see is something
of this sort:

class Person {
private:
Type stuff;
really_private:
OthertypeType valueable_stuff;
public:
Person(Person& from) : stuff(from.stuff),
// now, pay attention here:
valueable_stuff(
from.grant_special_access(this, &Person::valueable_stuff)
)
{}

template<class PM, class To> PM grant_special_access(To t, PM what)
{
// some fancy mechanism
// checking if 't' has access to _my_ privates
if (granted)
return what;
else
return 0;
}
};

Which actually looks like a security mechanism for safeguarding private
data members on per-instance basis. It's possible. Is it needed? Not
in our everyday programming activities. When is it needed? Well, when
some fancy system with inter-instance barriers is created... The language
does not have those things built-in. 'private' is not "per-instance", it
is "per-class". For whatever reason the OP thought otherwise.

Forming certain expectations before actually _learning_ the language, i.e.
coming to the language with preconceptions of how certain things should
behave, is often the cause of confusion and learning problems. One often
needs to "un-learn" things before one's ready to absorb real knowledge.
Nowadays we don't get asked often, but I remember when every fifth post
here was "what operator do I use to calculate Yth power of X? X^Y or
X**Y don't seem to work!"

V
 
D

Daniel T.

"Siam said:
I read somewhere that private members of some instance of class A are
accessible from another instance of the same class. In other words,
access to private members is done 'on a class by class basis, not on an
instance by instance basis'. I couldn't quite believe it, and tried it
out for myself, and was shocked to see it was true...
What is the reasoning behind this?

The individual who wrote the class is in the best position to know if
one object of the class should be able to modify the data of another
object of the class.
Why should one Person object be able to see another Person object's
private parts?

Why shouldn't this be the case?
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top