473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Accessing private members

Hi,

In the following code, I wonder how a private member of the class is
being accessed. The code compiles well in Visual Studio 6.0.

class Sample
{
private:
int x;
public:
void set(Sample& obj) {
x = x + obj.x; //=> private member accessed
}
};
int main(){
Sample obj1;
Sample obj2;
obj1.set(obj2);
return(0);
}

Thanks,
Sandeep
Jul 22 '05 #1
5 2387
Sandeep wrote:
Hi,

In the following code, I wonder how a private member of the class is
being accessed. The code compiles well in Visual Studio 6.0.
I'm not quite sure I understand your problem. Do you think the compiler
shouldn't accept it? The code is fine. The private member x of class
Sample are only accessed from a member function of Sample. If that
weren't allowed, how could the private member variable used at all?
class Sample
{
private:
int x;
public:
void set(Sample& obj) {
x = x + obj.x; //=> private member accessed
}
};
int main(){
Sample obj1;
Sample obj2;
obj1.set(obj2);
return(0);
}

Jul 22 '05 #2
On 1 Aug 2004 02:37:34 -0700, Sandeep <sa*************@yahoo.com> wrote:
Hi,

In the following code, I wonder how a private member of the class is
being accessed. The code compiles well in Visual Studio 6.0.

class Sample
{
private:
int x;
public:
void set(Sample& obj) {
x = x + obj.x; //=> private member accessed
}
};
int main(){
Sample obj1;
Sample obj2;
obj1.set(obj2);
return(0);
}

Thanks,
Sandeep


The code is fine, you are misunderstanding the purpose of private members.

The point of private members is that only the author of a class should
know how to use them. Any body else must use the public interface of the
class. The author of the Sample class wrote the Sample::set method so he
or she must know that accessing the private member obj.x is OK, even
though obj.x is part of a different object, its still a Sample object.

That's the point of private, its not to prevent one object from accessing
the internals of another object, its to prevent the users of a class from
accessing the internals of that class, it should not prevent the author of
a class from doing so.

john
Jul 22 '05 #3
On 8/1/2004 11:37 AM, Sandeep wrote:
void set(Sample& obj) {
x = x + obj.x; //=> private member accessed
}

I'm not sure I understand what is the problem.
I gues you are trying to understand what does this assignment do.

x = x + obj.x

means the same as

x += obj.x

obj.x is added to x and the result is assigned to x variable.
In other words, x is incremented by obj.x

Greets

--

Mateusz Łoskot
mateusz at loskot dot net
Jul 22 '05 #4

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:ce*************@news.t-online.com...
Sandeep wrote:
Hi,

In the following code, I wonder how a private member of the class is
being accessed. The code compiles well in Visual Studio 6.0.


I'm not quite sure I understand your problem. Do you think the compiler
shouldn't accept it? The code is fine. The private member x of class
Sample are only accessed from a member function of Sample. If that
weren't allowed, how could the private member variable used at all?
class Sample
{
private:
int x;
public:
void set(Sample& obj) {
x = x + obj.x; //=> private member accessed
}
};
int main(){
Sample obj1;
Sample obj2;
obj1.set(obj2);
return(0);
}



I think the question was about why the "obj.x" part is legal, not the use of
x directly. The object obj is a different object, not the one pointed to by
this->. It's of the same type, but it's not *this* object! So, a natural
question arises about the privacy of obj's members.

If obj were of some other type, then set() would obviously not have access
to its private members (excluding the possibility of friendship, of course).
But it's not readily apparent whether a member function should have access
to private members of *other* instances of the same class.

And I believe the answer is, yes, it does have such access.

Why C++ was designed that way, I don't know. I (at one time) would have
assumed privacy applied in this case also, but if I understand it correctly,
the issue of privacy is applied completely separately from any information
about the actual instance of the class, and only applies to the class type
itself. Thus, *any* instance of an object can access any other instance's
private members, providing they're of the same class.

A quote I read somewhere put it well: "A private member can only be accessed
by member functions of the class (and its friends)." Note that it says
"class", not "object" or "instance". Thus, acces is granted to all
instances of the class, not just the instance in question.

Likewise, protected member status is not related to the actual instance, but
to the class hierarchy itself.

(Is all this talk of accessing one's private members turning anyone else on,
or am I just the sick pervert I think I am? :-))

-Howard


Jul 22 '05 #5
Howard wrote:
[snip]
If obj were of some other type, then set() would obviously not have access
to its private members (excluding the possibility of friendship, of course).
But it's not readily apparent whether a member function should have access
to private members of *other* instances of the same class.

And I believe the answer is, yes, it does have such access.
And you are right.

Why C++ was designed that way, I don't know.


Assume the opposite (that is: no access)
Now write a copy constructor and/or assignment operator.
You end up with lots of getter functions :-)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: red | last post by:
If I use PEAR to produce an accociative array from a mysql table, and display the array with print_r, it says it is an array and it displays values like a regular array: Array ( => 1 =>...
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
6
by: harry | last post by:
Hi ppl I have a question about memory layout of a class. Consider the code below: class Base1 { virtual void f() { cout << "Base1::f" << endl; } virtual void g() { cout << "Base1::g" <<...
9
by: Joel Moore | last post by:
I'm a little confused here. If I have the following: Public ClassA Friend varA As Integer Private varB As Integer Private ClassB Public Sub MethodA() ' How can I access varA and varB here?...
4
by: John | last post by:
I am having a problem accessing the members in a structure via an interface and Class. ***Development Code*** Structure Person Public Name as String End Structure Interface IData Public...
6
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
2
by: mickey22 | last post by:
Hi all, I have some data members and member functions that are declared as private in a class A. Now I want to use these data members and functions(which are private) in my new C++ source...
2
by: agendum97 | last post by:
Is there a way of constructing a "class" in JavaScript to access a private member of a different scope of the same type? It is important that the variable remain private, and not accessible in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.