Copy Constructor Question

See the highlighted portion (in yellow) the definition of copyConstructor at http://libraryofcprograms.blogspot.com/2013/03/copyconstructor.html
Why haven't we used obj.getX() ?

1
2
3
void a::copyConstructor(a &obj){
    x  =  obj.x; //should have used obj.getX() because we're accessing a private member of obj?
}
Special rule. A class can access the private members of its own objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

class A {
public:

    explicit A(int data = 0): data(data)
    {
    }

    void show(const A &a) const
    {
        std::clog << a.data << '\n';
    }

private:

    int data;
};

class B {
public:

    void cant_show(const A &a) const
    {
        std::clog << a.data << '\n';
    }
};

int main()
{
    A target(555);
    A test_a;
    B test_b;

    test_a.show(target);
    test_b.cant_show(target);
}
Topic archived. No new replies allowed.