static function calling nonstatic method ? (or something like that)

Discussion in 'C++' started by Lucy Ludmiller, Sep 1, 2006.

  1. I have code like this :

    void foo(int i, void* p) { .. }


    class A
    {
    public:
    A();
    ~A();

    private:
    void foobar(int i, char* c);
    };



    Is there a way for foo to call foobar() ?
     
    Lucy Ludmiller, Sep 1, 2006
    #1
    1. Advertising

  2. Lucy Ludmiller wrote:
    > I have code like this :
    >
    > void foo(int i, void* p) { .. }
    >
    > class A
    > {
    > public:
    > A();
    > ~A();
    > private:
    > void foobar(int i, char* c);
    > };
    >
    > Is there a way for foo to call foobar() ?


    Sure:

    void foo(int i, void* p)
    {
    A a;
    a.foobar(i, static_cast<char*>(p));
    }

    But if your question is, can foo call foobar without an instance of
    class A to call it with, the answer is no, unless you change foobar to
    be a static member of A.

    Best regards,

    Tom
     
    Thomas Tutone, Sep 1, 2006
    #2
    1. Advertising

  3. Lucy Ludmiller

    Radu Guest

    Thomas Tutone wrote:
    > Lucy Ludmiller wrote:
    > > I have code like this :
    > >
    > > void foo(int i, void* p) { .. }
    > >
    > > class A
    > > {
    > > public:
    > > A();
    > > ~A();
    > > private:
    > > void foobar(int i, char* c);
    > > };
    > >
    > > Is there a way for foo to call foobar() ?

    >
    > Sure:
    >
    > void foo(int i, void* p)
    > {
    > A a;
    > a.foobar(i, static_cast<char*>(p));
    > }


    .... if class A declares foo as a friend:
    class A
    {
    friend void foo(int i, void* p) ;
    //.....
    };

    HTH
    Radu

    >
    > But if your question is, can foo call foobar without an instance of
    > class A to call it with, the answer is no, unless you change foobar to
    > be a static member of A.
    >


    you can declaring
    > Best regards,
    >
    > Tom
     
    Radu, Sep 1, 2006
    #3
  4. Radu wrote:
    > Thomas Tutone wrote:
    > > Lucy Ludmiller wrote:
    > > > I have code like this :
    > > >
    > > > void foo(int i, void* p) { .. }
    > > >
    > > > class A
    > > > {
    > > > public:
    > > > A();
    > > > ~A();
    > > > private:
    > > > void foobar(int i, char* c);
    > > > };
    > > >
    > > > Is there a way for foo to call foobar() ?

    > >
    > > Sure:
    > >
    > > void foo(int i, void* p)
    > > {
    > > A a;
    > > a.foobar(i, static_cast<char*>(p));
    > > }

    >
    > ... if class A declares foo as a friend:
    > class A
    > {
    > friend void foo(int i, void* p) ;
    > //.....
    > };


    Good point - I missed that foo was private. Thanks for the
    clarification.

    Best regards,

    Tom
     
    Thomas Tutone, Sep 1, 2006
    #4
    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. Pedro López
    Replies:
    0
    Views:
    433
    Pedro López
    Oct 13, 2003
  2. Jinesh

    Static/NONstatic error!!

    Jinesh, Jan 5, 2004, in forum: C++
    Replies:
    8
    Views:
    11,862
    Heinz Ozwirk
    Jan 6, 2004
  3. Replies:
    3
    Views:
    535
  4. The|Godfather
    Replies:
    7
    Views:
    3,972
    Miles Bader
    Oct 24, 2006
  5. Jeffrey
    Replies:
    7
    Views:
    426
    James Kanze
    Sep 30, 2008
Loading...

Share This Page