Can't access private class members inside of static method?

17,047

You cannot access a non static member inside a static method unless you explicitly make available the object instance inside the member function.(Pass object instance explicitly as argument or use a global instance which can be accessed inside the function)

For a non static member function an implicit this pointer is passed as the first argument to the function. The this pointer is dereferenced inside the member function to access the members. static members are not passed with the implicit this pointer so you cannot access non static members inside the function unless you explicitly get the object inside the member function.

Share:
17,047
xcdemon05
Author by

xcdemon05

Updated on June 04, 2022

Comments

  • xcdemon05
    xcdemon05 almost 2 years

    I have the following setup:

    //.h
    class Cell
    {
    private:
        POINT   mCellStartingPoint;
        int     mXoffset;
        int     mYoffset;
    public:
        static void DrawRowOfPixels(int yoff);
        Cell();
        ~Cell();
    };
    
    //.cpp
    void Cell::DrawRowOfPixels(int yoff)
    {
        HDC dc = GetDC(NULL);
        COLORREF red = 0xFF0000;
        for(int i = mCellStartingPoint.x; i < mXoffset; i++)
        {
            SetPixel(dc, mCellStartingPoint.x + i, mCellStartingPoint + yoff, red);
        }
    }
    

    However, when implementing the DrawRowOfPixels() method in the .cpp file, I get errors at all of the member variables of the Cell class. (i.e. mCellStartingpoint, mXoffset, and mYoffset)

    error C2228: left of '.x' must have class/struct/union

    error C2597: illegal reference to non-static member 'Cell::mXoffset'

    error C3867: 'Cell::mXoffset': function call missing argument list; use '&Cell::mXoffset' to create a pointer to member

    error: A nonstatic member reference must be relative to a specific object

    I know I'm probably doing something really stupid, but what's going on here? Why can't I use my private member variables inside my static member function like I should be able to?

    • chris
      chris about 11 years
      There's a logical reason static member functions can't access non-static data members. It's part of the definition of a static member function.
    • WhozCraig
      WhozCraig about 11 years
      It would likely help immensely if you had an object to go with those data members. You're using a static class function. It has no object, therefore no members. Make the function non-static, pass an object in as a parameter, or make the members static. any of the above will work. Choose the right one to fit your need.
    • xcdemon05
      xcdemon05 about 11 years
      So I literally can't access them then? I thought the point of a static function was to be able to be called without an object but still have the same use as if it were called by an object.
    • chris
      chris about 11 years
      @user2057387, If it has no object, which object's set of data members does it use?
    • xcdemon05
      xcdemon05 about 11 years
      Why do these things only make sense after I make an idiot of myself on the internet...
    • warunanc
      warunanc about 11 years
      @EdS. it was an honest mistake, my comment was meant for the OP
    • warunanc
      warunanc about 11 years
      you can't achieve what you are trying to achieve without making your data members static. Better understanding of basics would help you a lot, read and learn: cprogramming.com/tutorial/statickeyword.html