Can a static member function access non-static member? - c++

This is a discussion on Can a static member function access non-static member? - c++ ; Hello everyone! Can a static member function access non-static member? I think it is illegal.Is it right?...

+ Reply to Thread
Results 1 to 4 of 4

Can a static member function access non-static member?

  1. Default Can a static member function access non-static member?

    Hello everyone! Can a static member function access non-static member?
    I think it is illegal.Is it right?

  2. Default Re: Can a static member function access non-static member?

    On Dec 5, 9:27 am, dolphin <jdxyw2...@gmail.com> wrote:
    > Hello everyone! Can a static member function access non-static member?
    > I think it is illegal.Is it right?


    Write a 10-line test program and see

  3. Default Re: Can a static member function access non-static member?

    dolphin wrote:
    > Hello everyone! Can a static member function access non-static member?
    > I think it is illegal.Is it right?


    Yes, provided it has a pointer or reference to the object. Why
    shouldn't it?

    --
    Ian Collins.

  4. Default Re: Can a static member function access non-static member?

    On 2007-12-05 02:27:21 -0500, dolphin <jdxyw2004@gmail.com> said:

    > Hello everyone! Can a static member function access non-static member?
    > I think it is illegal.Is it right?


    Just like any other member function, it has access to private members
    of objects of its class. Just like any other member function, it can
    access those members through a pointer or reference to an object of its
    type. Unlike non-static member functions, it doesn't have an implied
    object to work with.

    class C
    {
    public:
    void member_func(C *ptr);
    static void static_member_func(C* ptr);
    private:
    int i;
    };

    void C::member_func(C *ptr)
    {
    ptr->i = 3; // access member of object pointed to by ptr
    i = 3; // access member of object pointed to by 'this'
    }

    void C::static_member_func(C *ptr)
    {
    ptr->i = 3; // access member of object pointed to by ptr
    }

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)


+ Reply to Thread

Similar Threads

  1. static member function and const
    By Application Development in forum c++
    Replies: 9
    Last Post: 11-19-2007, 04:40 AM
  2. Static local variable in an inline static member function
    By Application Development in forum c++
    Replies: 7
    Last Post: 11-01-2007, 04:28 PM
  3. static variable in non-static member function
    By Application Development in forum c++
    Replies: 5
    Last Post: 07-17-2007, 10:09 AM
  4. Replies: 3
    Last Post: 07-06-2007, 07:50 AM
  5. COM & static member function
    By Application Development in forum DOTNET
    Replies: 2
    Last Post: 09-05-2006, 10:14 AM