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?
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?
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.
You will need some existing object to call its non-static member function or access its non-static data member.
The typical use case for this would be handing off C API callbacks to object instances in a C++ project.
Since people are hell-bent on downvoting, here is the summary:
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.Look at the Singleton/factory method pattern -- they'll be of interest to you.
No, thats not possible unless you can somehow gain access to an instance of the defining class.
YES - you can, and here is how
This is the sort of a design, recursion aside, demonstrates how to call both static and non-static member functions.