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

Discussion in 'C++' started by rajasekaran.psg@gmail.com, Jan 5, 2007.

  1. Guest

    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..
    };
    , Jan 5, 2007
    #1
    1. Advertising

  2. Rolf Magnus Guest

    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?
    Rolf Magnus, Jan 5, 2007
    #2
    1. Advertising

  3. Ondra Holub Guest

    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.
    Ondra Holub, Jan 5, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    19
    Views:
    790
    Daniel T.
    Feb 4, 2004
  2. JustSomeGuy
    Replies:
    5
    Views:
    896
    JustSomeGuy
    Dec 5, 2004
  3. Andre  Eisenbach
    Replies:
    6
    Views:
    338
    David White
    Sep 11, 2005
  4. Anjo Gasa
    Replies:
    2
    Views:
    440
    Rolf Magnus
    Feb 27, 2006
  5. Hicham Mouline
    Replies:
    1
    Views:
    352
    Vladyslav Lazarenko
    Mar 27, 2009
Loading...

Share This Page