shaanxxx wrote:
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.