0

Actually, I'm at home in java, so please forbear with me. Having this MWE:

#include <iostream>

using namespace std;

class MyClass {
    private:
    double calculated;

    public:
    MyClass(double x=0) : calculated(42*x) {};

    void print() {
        cout << "calculated=" << calculated << endl;
    };

    void printManipulated() {
      MyClass obj;
      obj.calculated = 74574; // why is this possible?
      obj.print();
    };
};

int main() {
    MyClass obj;
    obj.printManipulated();
    return 0;
}

The snippet works and the console prints

calculated=74574

But why there are no errors? Why can I access the variable calculated in the method printManipulated unless it is declared private?

ATW
  • 233
  • 2
  • 14
  • 2
    Inside of `MyClass` you can access everything from the `MyClass` class even in a different instance. – drescherjm Nov 15 '20 at 17:43
  • Why do you expect classes to be unable to access their own private member variables? `printManipulated` is a `MyClass` function, it can access `MyClass` private members. – Nathan Pierson Nov 15 '20 at 17:43
  • Classes are the units of object-oriented design. The API is the boundary for encapsulation: private data members may not be accessed directly by client code with `MyClass` objects. The public API - like `printManipulated`, is a trusted part of `MyClass` though and can access all its members. – Tony Delroy Nov 15 '20 at 17:45
  • @NathanPierson yes but in ```printManipulated```, I create a new instance of ```MyClass```, so I shouldn't be able to access private members of that object, should I? I do not access the member of the current class but the private member of an instance, so I would not expect to be able to do so. – ATW Nov 15 '20 at 17:45
  • 1
    ***I shouldn't be able to access private members of that object, should I?*** In c++ the language says you should have full access. – drescherjm Nov 15 '20 at 17:47
  • 4
    @ATW: that is a common point of confusion. The programmer who writes the `MyClass` code is trusted to consider how multiple `MyClass` objects should properly interact: they can access the private parts of any of them. This is often necessary - for example, move assignment operators may change the moved-from object in ways not possible through the public API. – Tony Delroy Nov 15 '20 at 17:47
  • 1
    For what it's worth, Java has this exact same behavior. – Nathan Pierson Nov 15 '20 at 17:48
  • 1
    Also worth noting that access specifiers are not an absolute straightjacket. They serve to prevent (many, if not most) accidents and can easily be worked around deliberately. – user4581301 Nov 15 '20 at 17:49
  • @NathanPierson Holy Shit. You're right. I was never aware of that in Java. Thanks for pointing out. – ATW Nov 15 '20 at 17:52
  • where would you access the member if not inside methods of the class? – 463035818_is_not_an_ai Nov 15 '20 at 17:53
  • I was interpreting ```private: double calculated``` like ```object.calculated``` does NEVER give you access to the member because object does not offer this property because it is declared as private in the actual object. But as Nathan pointed out, even in Java, this case would compile without errors. – ATW Nov 15 '20 at 17:56

0 Answers0