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

access private member function

P: n/a
is there any way we can access private member function of
a class from outside.

i know we can access private member data using void pointer
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Aakash Joshi wrote:
is there any way we can access private member function of
a class from outside.
Only if the function from which you're trying to access the
private member is declared a friend or is a member of a class
that is declared a friend.
i know we can access private member data using void pointer


Huh?
Jul 22 '05 #2

P: n/a
On 24 Aug 2004 05:44:09 -0700, ja*****@rediffmail.com (Aakash Joshi)
wrote:
is there any way we can access private member function of
a class from outside.

i know we can access private member data using void pointer


The best way to do it is to make the class you want to access the
function a friend - note that you can do this even if the class in
question is in a library. Adding a friend declaration to a class
doesn't require it to be recompiled.

If you can't modify the header for some reason, then the usual
technique is to add:

#define private public

before including the header. Obviously this doesn't work if the
function is at the top of the class, before any private:.

Tom
Jul 22 '05 #3

P: n/a

"tom_usenet" <to********@hotmail.com> skrev i en meddelelse
news:r2********************************@4ax.com...
On 24 Aug 2004 05:44:09 -0700, ja*****@rediffmail.com (Aakash Joshi)
wrote:
is there any way we can access private member function of
a class from outside.

i know we can access private member data using void pointer


The best way to do it is to make the class you want to access the
function a friend - note that you can do this even if the class in
question is in a library. Adding a friend declaration to a class
doesn't require it to be recompiled.

If you can't modify the header for some reason, then the usual
technique is to add:

#define private public

before including the header. Obviously this doesn't work if the
function is at the top of the class, before any private:.

Tom


And not portable, so far as i know? Isn't the compiler allowed to rearrange
members at a protection-boundary?

/Peter
Jul 22 '05 #4

P: n/a
tom_usenet wrote:
On 24 Aug 2004 05:44:09 -0700, ja*****@rediffmail.com (Aakash Joshi)
wrote:

is there any way we can access private member function of
a class from outside.

i know we can access private member data using void pointer

The best way to do it is to make the class you want to access the
function a friend - note that you can do this even if the class in
question is in a library. Adding a friend declaration to a class
doesn't require it to be recompiled.

AFAIK, that is undefined
If you can't modify the header for some reason, then the usual
technique is to add:

#define private public

before including the header. Obviously this doesn't work if the
function is at the top of the class, before any private:.

ehm.. ever heard of the ONE-definition rule?

If you don't have the source of a library, don't change the headers that
come with it, and don't try to get access to members the authors didn't
want you to have access to: you might be breaking invariants without
knowing it.

If you do have access to the sources, make sure you don't break anything
by accessing the private members you're accessing and/or add accessors
and mutators to the class that keep the invariants in tact - consult
with the original author if you can and document your interpretation of
the class' invariants in case you break any you hadn't found/didn't know
about.

rlc
Jul 22 '05 #5

P: n/a
On Tue, 24 Aug 2004 20:54:30 +0200, "Peter Koch Larsen"
<pk*****@mailme.dk> wrote:

"tom_usenet" <to********@hotmail.com> skrev i en meddelelse
news:r2********************************@4ax.com.. .
On 24 Aug 2004 05:44:09 -0700, ja*****@rediffmail.com (Aakash Joshi)
wrote:
>is there any way we can access private member function of
>a class from outside.
>
>i know we can access private member data using void pointer


The best way to do it is to make the class you want to access the
function a friend - note that you can do this even if the class in
question is in a library. Adding a friend declaration to a class
doesn't require it to be recompiled.

If you can't modify the header for some reason, then the usual
technique is to add:

#define private public

before including the header. Obviously this doesn't work if the
function is at the top of the class, before any private:.

Tom


And not portable, so far as i know? Isn't the compiler allowed to rearrange
members at a protection-boundary?


Of course, but I don't know of any that actually do. So I think it's
portable, just not standard, if you see the distinction.

Tom
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.