Solved

make partial public access from parent class

Posted on 2004-09-17
7
167 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java inner class, for the sole use of parameter passing 21 76
Lambda for random numbers problem 7 107
How to split this in C++ 4 94
designing in object programming 12 52
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…
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 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++.

863 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

23 Experts available now in Live!

Get 1:1 Help Now