Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this private member accessible?

Tags:

I have this class:

class C {     private String msg;      public void F(C obj, String arg)     {         obj.msg = arg; // this is strange, the msg shouldn't be accessible here.     }      public void Output()     {         Console.WriteLine(msg);     } } 

The test code is:

C obj1 = new C(); C obj2 = new C(); obj1.F(obj2, "from obj1"); obj2.Output(); 

The Output is:

from obj1 

So, obj2's private member is accessed from another object obj1. I think this is kind of strange.

ADD

Here is an useful link mentioned by Habib:

Why are private fields private to the type, not the instance?

like image 360
smwikipedia Avatar asked Feb 14 '14 14:02

smwikipedia


People also ask

How private data members of a class are accessible outside?

Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.

Can an object access private members?

This is perfectly legal. Objects of the same type have access to one another's private members. This is because access restrictions apply at the class or type level (all instances of a class) rather than at the object level (this particular instance of a class).

How do you access the private members of a class in a derived class?

Private members of the base class cannot be used by the derived class unless friend declarations within the base class explicitly grant access to them.

What is the use of private members?

A function declared inside the class's private section is known as "private member function". A private member function is accessible through the only public member function. (Read more: data members and member functions in C++).


2 Answers

// this is strange, the msg shouldn't be accessible here.

private members are accessible inside the class, they are not accessible outside the class.

Private - MSDN

The private keyword is a member access modifier. Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared

For your other question:

So, obj2's private member is accessed from another object obj1. I think this is kind of strange.

You are passing address of obj2 to an instance method of obj1, and then accessing obj2's private member msg in the method and changing its value. But since both of them are of same type, you get the impression that you are accessing other class private member.

Try it with two different classes and you will be able to understand it better.

Consider if you have another class defined as:

class B {     public void SomeMethod(C obj, string arg)     {         obj.msg = arg; // that is an error.      } } 

now you can't access private member msg since you are trying to access it outside of the class, in your example, you are accessing the class member inside the class.

There could be an argument that why C# allows instance.PrivateMember inside the class, the language designers could have restricted the usage of private to this.PrivateMember, so that the private member is only accessible with the current instance. If that would have been the case then your code would raise the error on obj.msg = arg;. Apparently the C# designers chosen the private to instance access instead of private to current instance only, so the basic rule is that private members are accessible inside the class, whether with the this (current instance) or with an instance of same type. For more discussion why this was done, you can see this question

like image 61
Habib Avatar answered Sep 26 '22 03:09

Habib


If i understand private correctly, it means access only from within the same class, but not only from the same instance of that class.

like image 36
Sascha Rambeaud Avatar answered Sep 25 '22 03:09

Sascha Rambeaud