473,566 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2402
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 misunderstandin g 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
3914
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 => image/gif => buffaloa.gif => 153473
5
3638
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 parent class, and I can't make some "helper" class a friend of the parent class to help in accessing the data.) Problem: I want to derive a...
2
2286
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 in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the...
6
2603
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" << endl; } }; class Drive : public Base1{
9
3677
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? End Sub
4
1414
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 Property Employee As Person
6
5023
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
2
2588
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 file(.cpp file).I have created pointer to this class A, and tried to use the functions but I am getting an errror that: cannot access private data...
2
1583
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 anyway publically. For example: function SomeObj(privateKey) { this.process = function(someObj) { if (!(someObj instanceof SomeObj))
0
7666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.