Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Make inherited protected to public

Posted on 2002-04-13
5
Medium Priority
?
188 Views
Last Modified: 2010-04-02
Is ther any way to make inherited protected members to public members?
0
Comment
Question by:hestercybil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 600 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

688 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