Connecting Tech Pros Worldwide Help | Site Map

how to access a private member function without using friend and virtual

  #1  
Old January 5th, 2007, 09:35 AM
rajasekaran.psg@gmail.com
Guest
 
Posts: n/a
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:
void show() {cout<<"Private function called";} //
this is the function i need to be called

// dont change the class..
};

  #2  
Old January 5th, 2007, 09:45 AM
Rolf Magnus
Guest
 
Posts: n/a

re: how to access a private member function without using friend and virtual


rajasekaran.psg@gmail.com wrote:
Quote:
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:
void show() {cout<<"Private function called";} //
this is the function i need to be called
>
// dont change the class..
};
Why?

  #3  
Old January 5th, 2007, 10:15 AM
Ondra Holub
Guest
 
Posts: n/a

re: how to access a private member function without using friend and virtual


rajasekaran.psg@gmail.com napsal:
Quote:
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:
void show() {cout<<"Private function called";} //
this is the function i need to be called
>
// dont change the class..
};
Hi. This function is private to prevent its call from outside of class
A. There is no clean way how to call this function.

You could do a dirty hack:

#define private public
class A
{
private:
void show() {cout<<"Private function called";} //
};
#undef private

But it is not actually "not changing" the class.

Nobody should attempt to call private function directly - 'private' is
there to prevent it. As class user you should forget, that any private
function even exists, because it may require some special
pre-/postcondition to be fulfilled before/after it is called.

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
getters and setters kelvSYC answers 32 July 23rd, 2005 06:23 AM
access to private members of internal class Shea Martin answers 28 July 22nd, 2005 10:12 AM
access to private members of internal class Shea Martin answers 28 July 22nd, 2005 09:42 AM
problems with standard header Ryan D. Lucey answers 6 July 19th, 2005 05:38 PM