Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to access a private member function without using friend and virtual

Reply
Thread Tools

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

 
 
rajasekaran.psg@gmail.com
Guest
Posts: n/a
 
      01-05-2007
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..
};

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      01-05-2007
wrote:

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

 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      01-05-2007
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
private member function or non-member utility function ittium C++ 5 01-12-2012 08:56 AM
private static member access by a friend class Anjo Gasa C++ 2 02-27-2006 05:17 PM
Can't access a static private data member from a friend function? JustSomeGuy C++ 5 12-05-2004 05:51 PM
Should 'public virtual' always become 'private virtual'? & using private inheritance qazmlp C++ 19 02-04-2004 12:37 AM



Advertisments