Topic: Is a class its own friend?
Author: Cristian Georgescu <Cristian.Georgescu@worldnet.att.net>
Date: 1998/03/11 Raw View
Q: Can a class member function access the private data from the same
class but belonging to another object?
That is, is the following code ok?:
class A {
private: int data;
public: void copy(const A& rhs) {data = rhs.data;};
};
I thought that the member function "copy" in class A is not allowed to
access the private data of another object of the same class, i.e.:
rhs.data (only data or A::data can be accessed).
However it seems that my compiler does not complain; and I have also
found an analog example in "C++ P.L. II ed. by Bjarne S., page 783, the
"Handle class...).
Cristian.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Matt Seitz" <mseitz@meridian-data.com>
Date: 1998/03/11 Raw View
Cristian Georgescu wrote in message
<6e6scb$91p@bgtnsc02.worldnet.att.net>...
>Q: Can a class member function access the private data from the same
>class but belonging to another object?
A: Yes. The "private" label allows any object of the same class to access
to access the member. As Dr. Stroustrup puts it "The unit of protection is
the class, not the individual object." [THE DESIGN AND EVOLUTION OF C++,
section 2.10]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]