Non-static and static data and functions

Is it possible to call a non-static data element in a static member function? And can you also call a non-stationary member function in a static member function?

How do you do this?

+4
source share
9 answers

YES - you can, and here's how

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

This kind of design, recursion to the side, demonstrates how to call both static and non-static member functions.

+6
source

As people struggle to downvoting, here is a summary:

You can access a non-stationary member from a static member function if you pass an instance of the class, or a pointer to it or a reference. Qualification of an object (in other words, the static signature of an element) will determine whether it is possible to call only const or both functions const and non-const from the inside.

Non-static data / functions of an element depend on the this pointer, which is basically a pointer to an object that accesses / calls the data / functions of the element. Statics is a class level and is not associated with individual objects. If, however, you pass a reference / pointer to an instance of the class or the instance itself to a 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 << fx << std::endl; fx = 24; std::cout << fx << std::endl; std::cout << fy << std::endl; // you need non-const access for the following // so they don't work -- see foo signature //fy = -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 template - you will be interested in them.

+5
source

You need an instance of an object to call a non-static member function or access a non-stationary data element. Statics does not have this, so in general they cannot, unless they get it from somewhere (i.e. you have a global table that a static function uses to get a pointer to an object.)

But statics should not receive non-static data. If they should, they should not be static. Can you show us what you are trying to do?

+4
source

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

+2
source

Usually, if you do not have a static pointer to the instance.

The problem is that the static method does not have a specific instance on which it runs. You can call a non-stationary member function if you pass an instance, but not otherwise.

0
source

No, this is not possible if you cannot somehow access an instance of the defining class.

0
source

Yes for both:

 class A { int x; void f() { staticfunc(); } static void staticfunc() { A a; ax = 42; af(); } }; 

Of course, mutual recursion in the above case will cause several problems :-)

0
source

A typical use case for this is to pass 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 } }; 
0
source

static members are part of the class. These are data members that are not dependent on any other class variable. and since they are independent of other members of the class, so you can use the scope resolution operator with them. these are global class members. while non-static members are created for an instance of the class.

-1
source

All Articles