Solved

Make inherited protected to public

Posted on 2002-04-13
5
183 Views
Last Modified: 2010-04-02
Is ther any way to make inherited protected members to public members?
0
Comment
Question by:hestercybil
  • 3
5 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 6939662
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
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6939666
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
0
 
LVL 3

Expert Comment

by:GGRUNDY
ID: 6939677
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;
  }
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6939708
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
0
 
LVL 30

Accepted Solution

by:
Axter earned 150 total points
ID: 6939939
You can do the folowing:
class Base
{
protected:
     int data;
};

class Derive : protected Base
{
public:
     Base::data;
};


int main(int argc, char* argv[])
{
     Derive x;
     x.data = 3;
     printf("x = %i\n",x.data);

     system("pause");
     return 0;
}
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question