Solved

Make inherited protected to public

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

929 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

9 Experts available now in Live!

Get 1:1 Help Now