-2

why this code is working? I mean the value is a private variable, why function assign() can access it directly?

class A {
private:
    int value;
public:
    A() :value(0){ }
    void assign(A x)
    {
        value = x.value;
    }
};

int main()
{
    A a;
    A b;
    a.assign(b);

    return 0;
}
Roger ALex
  • 95
  • 5
  • 8
    Class methods can access private members of their own class. Private just means it can't be directly accessed from outside of the class. It would be rather useless if private meant "this can't be used by anybody". – Carcigenicate Oct 16 '17 at 12:43
  • 3
    your introductory book/class/tutorial surely explains the access modifiers. – bolov Oct 16 '17 at 12:45
  • And with recent edit, this aspect of the question is clear now. – Yunnosch Oct 16 '17 at 12:51
  • Yes, I forgot to call the method, now I add it to the code, and it still working. I cannot understand it clearly. @Yunnosch – Roger ALex Oct 16 '17 at 12:51
  • 1
    Possible duplicate of [Why do objects of the same class have access to each other's private data?](https://stackoverflow.com/questions/6921185/why-do-objects-of-the-same-class-have-access-to-each-others-private-data) – HostileFork says dont trust SE Oct 16 '17 at 12:57
  • 1
    After the intended improvement of question was achieved, I deleted my corresponding comment, in order to not confuse anybody, now that the question has been edited by OP. – Yunnosch Oct 16 '17 at 13:01
  • @HostileFork Yes, This is exactly what I want to know. Thanks a lot! – Roger ALex Oct 16 '17 at 13:04

3 Answers3

4

The private keyword means that no code outside the class can access the class.

Methods of the same class can access it, sure, because if they couldn't nobody could access private variables and they'd be useless.

If you worry about the access from the method called for A to the private member of B, don't worry.
The idea of private and public is making the implementation details of the class a thing nobody knows (and needs to know), but the class itself.
The class A "knows" how A itself is implemented, and so is "allowed" to use it's internals - privates, even if they're not of the called object.

Neo
  • 3,534
  • 2
  • 20
  • 32
2

Member functions (e.g., A::assign(A)) can access private members of their respective class, in addition to protected members of inherited classes.

Scott Mudge
  • 957
  • 6
  • 18
2

int value is private, but void assign(A x) is public. In main function (outside class A) you can access only to public methods. Inside class A (for example in assign method) you can access to public, private and protected members

For example: You can't write something like this:

A obj;
obj.value;

because value is private. But you can access it using other methods: for example you can define set(int val) method

class A {
private:
    int value;
public:
    A() :value(0){ }
    void set(int val) 
    { 
        value = val; 
    }
    void assign(A x)
    {
        value = x.value;
    }
};

int main()
{
    A obj;
    obj.set(10);
}
Gor Asatryan
  • 904
  • 7
  • 24