Accessing base class' private members | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Accessing base class' private members

So there is a way to do it. https://code.sololearn.com/cUtw294D90TY/?ref=app

25th Feb 2018, 5:54 PM
Drpopz
3 Answers
0
Ehm no. You cannot access the private members of a baseclass. You are accessing protected members, which is different. When you are designing your code, you shouldn't need protected members. There are ofcourse exceptions, but most cases I've seen (which are a lot!) were really a design issue. The thing is: all your classes should really protect its internal (private) data to make sure your classes' state isn't suddenly changed by someone else. With protected members you aren't really protecting your internal data, as anyone can inherit from your class and thus change stuff as they like. You should have a decent public interface for manipulating your object, that interface should be sufficient for child classes as well. Again: there are exceptions, but they are rare! My 2ct.
7th Mar 2018, 9:03 PM
Freddy
0
I understand what you say, but if you check the example code alleged in the original message you'll see what I meant, as the daughter class effectively has means to access a private member of the base class through means of a public method inherited from the base class. The moral of the story is that if you give a public interface to access a private member, derived classes will inherit it
10th Mar 2018, 4:11 PM
Drpopz
0
I am not sure you understand what I said. First: you're not using public methods in the daughter class, you use: cout <<"fricci:"<<this->fricci<<endl; fricci is a protected member variable of mother. You'd be using a public method of you used say(). Second: the moral of the story you write is not anywhere near the conclusion in your original post...
10th Mar 2018, 5:28 PM
Freddy