473,586 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

access private member function

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
5 6557
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
On 24 Aug 2004 05:44:09 -0700, ja*****@rediffm ail.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

"tom_usenet " <to********@hot mail.com> skrev i en meddelelse
news:r2******** *************** *********@4ax.c om...
On 24 Aug 2004 05:44:09 -0700, ja*****@rediffm ail.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
tom_usenet wrote:
On 24 Aug 2004 05:44:09 -0700, ja*****@rediffm ail.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
On Tue, 24 Aug 2004 20:54:30 +0200, "Peter Koch Larsen"
<pk*****@mailme .dk> wrote:

"tom_usenet " <to********@hot mail.com> skrev i en meddelelse
news:r2******* *************** **********@4ax. com...
On 24 Aug 2004 05:44:09 -0700, ja*****@rediffm ail.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
10812
by: Casey | last post by:
I have a custom control that allows user to place rtf content in it. The custom control has a private member (RichTextBox). I have a code that actually works: MyRTFControl myRtf; MemberInfo minfo = typeof(MyRTFControl).GetMember("richTextBox", BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase | BindingFlags.Instance |
9
2741
by: Mike | last post by:
Hi, Just a simple question: why the compiler doesn't report error when accessing a private member function inside a function having template type ? For example: #include<iostream> using namespace std;
2
1843
by: rajasekaran.psg | last post by:
hi there, i am a Rajasekaran, a final yr it student,, i am having a doubt regarding the above subject can you guys help me out,, eg: class A { private:
2
4154
by: rajasekaranrajasekaran | last post by:
hi,,, i am having a scenario plz help me to find a soln,, its really challenging for me hope it is useful 4 u 2.... I need to access a private member function witout using a friend, virtual and not modifying the class,, for example consided this class X { private: void show() // this is the fn i need to access public:
14
4184
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
3752
by: PengYu.UT | last post by:
Hi, I want to write a test function to test a class. The class has a private member function that need to be tested. Although I could modify the member function as protected or public, I do not want to modify the class definition. I'm wondering what is the standard way to test private member functions in a library. Thanks, Peng
4
1885
by: AngreGanon | last post by:
Hi, all~ ^^ If i use #1 OK, but use # 2 error happend what's different? #include <iostream> /* -----------# 1------------ using std::endl; using std::cout; using std::ostream; */------------------------------
2
1359
by: adithi | last post by:
A Variable is declared as "Private" in one class in C#. Can i access this same variable in other class which is derived by this? For eg: public class Teacher { private string Name="ABC"; }
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.