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

x
?
Solved

make partial public access from parent class

Posted on 2004-09-17
7
Medium Priority
?
174 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 1800 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 200 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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++.
Suggested Courses

670 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