Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making one of the inherited protected members private

class A
{
protected:
    int m_a;
    int m_b;
};

class B: public A
{
};

In class B i want to make m_a private. Does the following the correct way to do it

class B:public A
{    
private:
    int m_a;
};

Won't this result in 2 copies of m_a ?

like image 573
KodeWarrior Avatar asked Sep 20 '12 10:09

KodeWarrior


1 Answers

The correct way to adjust the access control of a member is with a using declaration:

class B: public A {    
private:
    using A::m_a;
}

Just writing int m_a; would indeed result in two copies of m_a, and a derived class would be able to access A's copy of m_a by writing A::m_a.

like image 98
ecatmur Avatar answered Sep 27 '22 19:09

ecatmur