Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access private members in inheritance

I have a class A, which have a field val declared as private. I want to declare a class B, that inherit from A and have an access to val. Is there a way to do it on C++?

I want to do it because I need to overload some functions of A, without changing A code at all.

Thanks.

like image 562
ofer Avatar asked Nov 23 '11 11:11

ofer


People also ask

Can inheritance access private members?

public, protected and private inheritance in C++ protected inheritance makes the public and protected members of the base class protected in the derived class. private inheritance makes the public and protected members of the base class private in the derived class.

How do you make private members accessible in inheritance?

You need to define it as protected . Protected members are inherited to child classes but are not accessible from the outside world. after inheriting protected members, you can provide public access. then we should avoid to keep personal data in protected access specifier.

How do you access private members of a parent class?

A class in C++ has public, private and protected sections which contain the corresponding class members. Protected members in a class are similar to private members as they cannot be accessed from outside the class. But they can be accessed by derived classes or child classes while private members cannot.

Can inherited class access private members java?

Private Members in a SuperclassA subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.


3 Answers

Quick answer: You don't. Thats what the protected key-word is for, which you want to use if you want to grant access to subclasses but no-one else.

private means that no-one has access to those variables, not even subclasses.

If you cannot change code in A at all, maybe there is a public/protected access method for that variable. Otherwise these variables are not meant to be accessed from subclasses and only hacks can help (which I don't encourage!).

like image 176
Constantinius Avatar answered Oct 07 '22 09:10

Constantinius


Private members of a base class can only be accessed by base member functions (not derived classes). So you have no rights not even a chance to do so :)

class Base

  • public: can be accessed by anybody
  • private: can only be accessed by only base member functions (not derived classes)
  • protected: can be accessed by both base member functions and derived classes
like image 26
Schehaider Aymen Avatar answered Oct 07 '22 11:10

Schehaider Aymen


Well, if you have access to base class, you can declare class B as friend class. But as others explained it: because you can, it does not mean it's good idea. Use protected members, if you want derived classes to be able to access them.

like image 10
dbrank0 Avatar answered Oct 07 '22 09:10

dbrank0