Link to home
Start Free TrialLog in
Avatar of hestercybil
hestercybil

asked on

Make inherited protected to public

Is ther any way to make inherited protected members to public members?
Avatar of DanRollins
DanRollins
Flag of United States of America image

You can write some geat/set access functions, but the whole idea of declaring a member variable 'protected' is to prevent public access to it.   Solution:  DOn't declare it 'protected' in the first place.

-- Dan
Oh yes, you can use 'friend' to (effectively) make public a member function that had been declared 'protected', but that is not available for member variables.

-- Dan
Avatar of GGRUNDY
GGRUNDY

Is this the sort of thing you are looking for.....?


class CA
{
  protected:
  int m_isVisible;
  };

class CB : protected CA
{
  public:
    using CA::m_isVisible;
  };

class CC
{
  public:
  void Method1();
  };

void CC::Method1()
{
  CB c;
  c.m_isVisible = true;
  }
Here's an idea:  You can make a reference to a variable that looks and acts like that protected variable:

class Base {
public:
     int nPub;
protected:
     int nProt;
};
class Derived:public Base {
public:
     Derived() : nProt(Base::nProt) { };
     int& nProt;
};

void main() {
     Derived oDerived;
     oDerived.nProt= 1;
}
=-=-=-=-=-
If it quacks, it *is* a duck.

-- Dan
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial