Solved

make partial public access from parent class

Posted on 2004-09-17
7
171 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_
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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.

808 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