Help on Inheritence needed!
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!
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
Alan
Quote:Original post by wild_pointerQuote: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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement