By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

private member

P: n/a
How can we modify private member in the derive class ?
is it possible ?
language provide any feature for this ?

Jan 3 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a

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.

Jan 3 '07 #2

P: n/a
"shaanxxx" <sh******@yahoo.comwrote in message
news:11*********************@v33g2000cwv.googlegro ups.com
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.

--
John Carson
Jan 3 '07 #3

P: n/a
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.
Jan 3 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.