Solved

make partial public access from parent class

Posted on 2004-09-17
7
173 Views
Last Modified: 2010-04-01
Is there any way to make it so I only give protected access to a some parent members?

I want to inherit a class, but I don't want my child class to be able to access all the members in the parent class.
The parent class has all the member functions as public, and it's too much work to change the parent class at this point.
0
Comment
Question by:Hollaceluna_
[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
7 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12089031
You can use the using clause.

using parent::myparent_function_name;

You put the above in your child class, and have your child class declare the parent class private.
0
 
LVL 30

Accepted Solution

by:
Axter earned 450 total points
ID: 12089053
Example code:

class parent
{
public:
      int func1(){return 1;}
      int func2(){return 2;}
      int func3(){return 3;}
      int func4(){return 3;}
      int func5(){return 3;}
};

class child : private parent
{
public:
      using parent::func1;
      int TestFunc2() {return func2();}
protected:
      using parent::func2;
};

int main(int argc, char* argv[])
{
      child c;
      cout << c.func1() << endl;
      cout << c.TestFunc2() << endl;

In the above example code, the child class declared the parent class as private.
Therefore by default, all the parent's members are not accessable to the child class.
However, by declaring certain members with the using method, you give the access you want to certain members.
In above example func1 is given public access and func2 is given protected access.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12090769
>>>> I don't want my child class to be able to access all the members in the parent class

The using clause  doesn't prevent the baseclass functions from being called by member functions of any derived class.

>>>> it's too much work to change the parent class at this point

Assuming you have a lot of running code using that baseclass and don't want to recompile/rebuild all that, i would recommend to create a new baseclass header file where you make the appropriate changes from public to private. As long as there are no other changes than that, modules can be compiled with either the old or new header file included. That is because public/private are compiler directives that have no influence on the object modules created.

Regards, Alex
 

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 50 total points
ID: 12091052
> The using clause  doesn't prevent the baseclass functions from being called by member functions of any derived class.

True. You'd need something like this:

class parent
{
    // ...
public:
     int func1(){return 1;}
     // ...
};

class restricted_child_interface : private parent
{
public:
     using parent::func1;
};

class child : public restricted_child_interface
{
};
0
 
LVL 30

Expert Comment

by:Axter
ID: 12091257
>>The using clause  doesn't prevent the baseclass functions from being called by member functions of any derived class.

If you use it with the method I posted, it does.

All you have to do is make the parent private, and then only use the using clause for members you want public or private, as I posted in my example code.

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12091491
>> of any derived class

Sorry, that wasn't precise.  I should have said "from any class directly derived from baseclass parent". You see, e. g. with

        int TestFunc2() {return func2();}

the child class has full access to any public parent members, what is in contradiction to that the asker wants

>>>> don't want my child class to be able to access all the members in the parent class

However, rstaveleys method to define a wrapper class does the job.

Regards, Alex
0
 

Author Comment

by:Hollaceluna_
ID: 12094980
Axter,
That works great, and simple too.

rstaveleys,
Your method is an interesting idea, but I was really looking for control from the current class.
However, I will keep your method in mine for future code.

Thanks both you and Axter.
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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

630 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