Can You Access Private Member Variables Across Class Instances

A private variable can be accessed from another object of the same type?

Methods of a class can access its private attributes throughout all class instances, at least in C++.

Accessing private member variables of a class from a static method

static myClassPtr create(unsigned int val) {

create() is a static method of myClass, it is a member of this class. As such it it entitled to access all private members and methods of its class. This right extends not only to its own class instance, but any instance of this class.

As per my understanding private members should not be accessible.

... except by members of their class.

Let's create a completely pointless copy constructor for your class, the same copy constructor you would get by default:

myClass(const myClass &o) : m_val{o.m_val} {}

This copy constructor has no issues, whatsoever, of accessing m_val of the passed-in object. Exactly the same thing happens here. m_val is a private member of its class. It doesn't mean that only an instance of the same exact object can access its private members, it means that any instance of the class, or a static class method, can access the private class members.

can you access private member variables across class instances?

Yes, any code within a class can access private data in any instance of the class.

This breaks encapsulation if you think of the unit of encapsulation as the object. C++ doesn't think of it that way; it thinks of encapsulation in terms of the class.

C++ - Why can a passed object's private variables be accessed directly in the Copy Constructor?

That's because access specifiers are effective per-class, not per-object. So a class method can access private members of any instance of the class.

All members of a class (bodies of member functions, initializers of member objects, and the entire nested class definitions) have access to all the names to which a class can access. A local class within a member function has access to all the names the member function itself can access.

can we access a private variable using an object

You can access i from within the class because private members can only be accessed by members of the class. In this case it looks strange because p is a different object than the object that accesses the variable, but its still the same class and the restriction is on the class level not on the object level.

class Program
{
private int i;

public void method1()
{
Program p = new Program();
p.i = 5; // OK when accessed within the class
}

}

You can not access i from within another class (unless it is an inner class but that's a different story). Which is completely as expected.

class AnotherClass
{
void method2()
{
Program p = new Program();
p.i = 5; //error because private variables cannot be accessed with an object which is created out side the class
}
}

I understand the point you want to make. The restriction on class level looks counter intuitively. And maybe this is wrong. But the member variables are still only accessible from within the class, so you still have total control to guarantee the encapsulation of your privates.

Difference between access of private instance variables in same class and in different class

The access scope does is not related to "which object accesses the value of what other object", but rather to "which code (from which class) accesses members (of objects) of a given class"

In your case, the constructor is able to access these variables directly because the class of the object passed to the constructor is the same. That means, it's the code from the class itself that accesses it. Based on this, private access is available. In other words, its the Cart class accessing private fields from the Cart class (the same way it would say this.id, for example).

And BTW, it's not just the constructor that has this privilege, any code inside the Cart class can do the same.

Check more information on this documentation page: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Why can private member variable be changed by class instance?

Private members are accessible to any code within the program text of that class (including within nested types). It has nothing to do with which instance of the class you're dealing with.

I don't believe this violates encapsulation - the API is still separated from the implementation, but the implementation "knows" about itself regardless of which instance it's looking at.

I believe that in some other languages this isn't how accessibility works, but it definitely is for C# and Java. (Java has slightly different rules about what can access private members, but the translated code for what you've written would still work.)



Related Topics



Leave a reply



Submit