access control in the copy constructor's initializer list

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

class Sales_item{
public:
Sales_item():p(0), use(new size_t(1)){}
Sales_item(const Item_base&);
Sales_item(const Sales_item&i): p(i.p),use(i.use) {++*use;}
~Sales_item(){decr_use();}
Sales_item& Sales_item::operator= (const Sales_item& rhs);
const Item_base *operator->() const{
    if (p) return p;
    else throw logic_error("unbound Sales_item");}

const Item_base& operator*() const {
    if (p) return *p;
    else throw logic_error("unbound Sales_item");}
private:
    Item_base *p;
    std::size_t *use;
void decr_use()
{if (--*use == 0) {delete p; delete use;}}

};
Sales_item& Sales_item::operator= (const Sales_item& rhs)
{
         ++*rhs.use;
         decr_use();
         p = rhs.p;
         use = rhs.use;
         return *this;

}



My question is, I declared use and p as private member, why, from the initializer list in the Copy constructor, it could direct refer to the private member?
the private keyword means that the members' visibility is restricted to class scope - it doesn't restrict access down to the individual object level.

In other words, two objects which are of the same class can access each other's implementation details, because those implementation details are effectively one and the same. (The same is true for static functions which belong to that class, although static functions which need access shouldn't really be static in the first place)

However, other functions from another external class, including base or derived classes, or standalone functions will not be able to see private implementation details (unless they are a friend of the class).
Last edited on
Topic archived. No new replies allowed.