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

  • Thread starter rajasekaran.psg
  • Start date
R

rajasekaran.psg

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..
};
 
R

Rolf Magnus

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?
 
O

Ondra Holub

(e-mail address removed) napsal:
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.
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top