non-static and static data and functions

2019-05-08 02:33发布

Is it possible to call a non-static data member in a static member function? And is it also possible to call a non-static member function in a static member function?

How do you do it?

标签: c++ static
9条回答
贼婆χ
2楼-- · 2019-05-08 03:12

Not normally, unless you have a static pointer to an instance.

The problem is that the static method doesn't have a particular instance on which it's working. You can call the non-static member function if you pass in the instance, but otherwise, no.

查看更多
beautiful°
3楼-- · 2019-05-08 03:16

You will need some existing object to call its non-static member function or access its non-static data member.

查看更多
放我归山
4楼-- · 2019-05-08 03:16

The typical use case for this would be handing off C API callbacks to object instances in a C++ project.

 // declared in some external C API
typedef void __stdcall EnumDataCallback( void* context, void* data );
void EnumData( EnumDataCallback* callback, void* context ); 

// in consuming C++ Project
class MyDataManager
{
public:
  void PollData()
  {
       ::EnumData( &MyObject::StaticEnumData, this );
  }
private:
  static void __stdcall StaticEnumData( void* context, void* data )
  {
    MyDataManager* instance = (MyDataManager*)(context);
    instance->EnumData( data );
  }
  void EnumData( void* data )
  {
    // actual callback code
  }
};
查看更多
等我变得足够好
5楼-- · 2019-05-08 03:18

Since people are hell-bent on downvoting, here is the summary:

You can access a non-static member from within a static member function provided you pass in a class instance, OR a pointer thereof OR a reference. The object's qualification (in other words, the static member signature) will determine whether you can call only const or both const and non-const member functions from within.

Non-static member data/functions rely on the this pointer -- which is basically a pointer to the object accessing/invoking the member data/function. Statics are class level and are not associated with the individual objects. If, however, you pass on a reference/pointer of a class instance/or an instance itself to the static function, you can make a call.

#include <iostream>
struct Eg {
 Eg() : x(42), y(-42) {}

static void foo(Eg const&f) {
    std::cout << "foo\n";
    f.bar();

    // we are good -- x is mutable
    std::cout << f.x << std::endl;
    f.x = 24;
    std::cout << f.x << std::endl;

    std::cout << f.y << std::endl;

    // you need non-const access for the following
    // so they don't work -- see foo signature
    //f.y = -24; compile error -- const l-value    

   // f.barbar(); same as above
}

void bar() const { // const required since we have a const reference
 std::cout << "bar\n";
}

void barbar() { // const required since we have a const reference
 std::cout << "bar\n";
}

  // note well the members
  mutable int x;
  int y;
};

int main() {
   Eg ex;

   Eg::foo(ex); // or ex.foo(ex);
}

Look at the Singleton/factory method pattern -- they'll be of interest to you.

查看更多
Fickle 薄情
6楼-- · 2019-05-08 03:22

No, thats not possible unless you can somehow gain access to an instance of the defining class.

查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-05-08 03:24

YES - you can, and here is how

class Foo
{
    public:
     static void staticFunc( const Foo &  foo)
     {
           foo.memberFunc();      

     }
      void memberFunc() const
      {
           staticFunc(*this);

      } 


}; 

This is the sort of a design, recursion aside, demonstrates how to call both static and non-static member functions.

查看更多
登录 后发表回答