How do you call a regular member function from a static member function?

Discussion in 'C++' started by aling, Oct 29, 2005.

  1. aling

    aling Guest

    How do you call a regular member function from a static member
    function? Any idea?
    aling, Oct 29, 2005
    #1
    1. Advertising

  2. aling

    Guest

    By definition, you can't.
    static member functions don't a particular instance of the class it
    resides in.
    usually, if you need an access to a specific instance, you must pass a
    pointer to the instance to the static function.
    , Oct 29, 2005
    #2
    1. Advertising

  3. aling

    TIT Guest

    Re: How do you call a regular member function from a static memberfunction?

    aling sade:
    > How do you call a regular member function from a static member
    > function? Any idea?
    >


    class A {
    public:
    void f(){}
    static void g() {
    A a;
    a.f();
    }
    };

    int main() {
    A::g();
    return 0;
    }

    TIT
    TIT, Oct 29, 2005
    #3
  4. Re: How do you call a regular member function from a static memberfunction?

    aling wrote:
    > How do you call a regular member function from a static member
    > function? Any idea?
    >


    Exactly the same way that you call a regular member function anywhere
    else. There is nothing special about static member functions in this regard.

    To call a regular member function you need an object of the appropriate
    type. Then you use that to call the regular member function. E.g.

    SomeObject obj;
    obj.some_function();

    The above code will work perfectly well in a static member function or
    anywhere else.

    john
    John Harrison, Oct 29, 2005
    #4
  5. "aling" <> wrote in message
    news:...
    > How do you call a regular member function from a static member
    > function? Any idea?
    >


    Is this a trick interview question or are you trying to write some code
    dependent
    on it, since this is likely to be a bad thing to do...post some code.

    dave
    Dave Townsend, Oct 29, 2005
    #5
  6. aling

    aling Guest

    Yes, this is a trick interview question .
    Dave Townsend wrote:
    > Is this a trick interview question or are you trying to write some code
    > dependent
    > on it, since this is likely to be a bad thing to do...post some code.
    aling, Oct 30, 2005
    #6
  7. why do this?
    use non-static member function instead.
    Xiaobin.Huang, Oct 30, 2005
    #7
    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. Victor Bazarov
    Replies:
    0
    Views:
    1,347
    Victor Bazarov
    Jun 23, 2003
  2. 0to60
    Replies:
    4
    Views:
    501
    jeffc
    Nov 21, 2003
  3. Replies:
    2
    Views:
    503
    Jonathan Mcdougall
    Aug 6, 2007
  4. dolphin
    Replies:
    3
    Views:
    1,323
    Pete Becker
    Dec 5, 2007
  5. A L
    Replies:
    12
    Views:
    2,014
Loading...

Share This Page