access private member function

A

Aakash Joshi

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
 
V

Victor Bazarov

Aakash said:
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?
 
T

tom_usenet

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
 
R

Ronald Landheer-Cieslak

tom_usenet said:
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
 
P

Peter Koch Larsen

tom_usenet said:
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
 
T

tom_usenet

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
 

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
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top