Sign in to follow this  

Help on Inheritence needed!

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I'm trying to get my head around the whole inheritence thing and had a question... Say I have a base class (base_class) that has a private variable (x). Now, say I derive a new class from base_class called my_class (class my_class: public base_class) that adds a new private variable (y), and a public function that sets both x and y to a value. The compliler tells me I don't have access to x: "cannot access private member declared in class 'base_class'. I thought that as the child class was declared with the 'public' function it would be able to use it's own member functions to access the private varibles inherited from the base class...? Can anyone clear this up? Cheers!

Share this post


Link to post
Share on other sites
It also depends on the status of the variable in the base class. Public variables remain public in derived classes, protected variables become private and private variables are not accessable.

Alan

Share this post


Link to post
Share on other sites
Quote:
Original post by wild_pointer
Quote:
Original post by AlanKemp
protected variables become private
Alan


I don't think so.


I really dont know what I was thinking when I wrote that...

Protected is what the OP was after. Protected is like private, except that derived classes can also access them.

Alan

Share this post


Link to post
Share on other sites

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this