private member

S

shaanxxx

How can we modify private member in the derive class ?
is it possible ?
language provide any feature for this ?
 
O

Ondra Holub

shaanxxx napsal:
How can we modify private member in the derive class ?
is it possible ?
language provide any feature for this ?

You can modify it with methods of parent class. Usualy are defined Get
and Set methods for such purpose.

You can also make such members protected and then are accessible from
all inherited classes, but not from outside.
 
J

John Carson

shaanxxx said:
How can we modify private member in the derive class ?
is it possible ?
language provide any feature for this ?


The base class must have a member function for making the modification that
the derived class can call. The member function will have to be protected or
public.
 
R

Rolf Magnus

shaanxxx said:
How can we modify private member in the derive class ?

Members are made private to _prevent_ anything except the class itself and
its friends from accessing them.
is it possible ?
language provide any feature for this ?

You can add a protected member function to the base class that modifies the
member variable. But I guess that you're asking that question because you
actually can't modify the base class. In this case, there is no portable
way I know. One hack that is rather ugly, non-portable and actually in
violation of the C++ standard would be:

#define private protected
#include "thebaseclassheader.h"
#undef private

There was one situation where I actually needed this, because I was using a
library, and one class member that was essential to my derived class was
private. I had the choice to either rewrite the whole class myself (and it
was a big and complicated one) or to use the above hack. I chose the hack,
even though it gave me a bad feeling in my stomach.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,529
Members
48,250
Latest member
Bette22B13

Latest Threads

Top