Solved

make partial public access from parent class

Posted on 2004-09-17
7
164 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

746 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