Solved

Make inherited protected to public

Posted on 2002-04-13
5
181 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now