2

I was wondering if a static member function essentially means that it gets an address in memory at compile-time and never changes it during the whole program.

Therefore it gives us the chance to pass it around in pointers without any fear as we are always sure that we will forever find it in the same place.

Satrapes
  • 183
  • 1
  • 18
  • The linked answer is for a static field, but applies here, too http://stackoverflow.com/questions/7525942/address-of-a-static-variable. Also: Why (non-static) member pointers are different: http://stackoverflow.com/questions/1485983/calling-c-class-methods-via-a-function-pointer – dhke Aug 24 '15 at 14:56

3 Answers3

2

all functions have static addresses assigned to them at compile time (it's a little bit different for dynamically loaded libraries). Member function (static or not) is a standard function, it's address is known at compile time. How else linker could work?

Hcorg
  • 11,598
  • 3
  • 31
  • 36
  • This doesn't take into account virtual function calling and the implicit `this` pointer. Non-static member functions are a bit more complicated in how they're handled. – Rob K Aug 24 '15 at 15:08
  • 2
    @Rob K: They are still functions with a fixed address. They only differ in the generated calling code (even virtual functions have a fixed address - they are just not called directly) – MikeMB Aug 24 '15 at 16:17
2
  • Using static function u can call constructor which is declared as a private.
  • Here is code,

    #include <iostream>
    
    using namespace std;
    
    class STATIC
    {
    private:
           STATIC()
           {
               cout<<"In Constructor..."<<endl;
           }
    public:
           static void fun()
           {
           STATIC a1;
           }
    };
    
    int main()
    {
        STATIC::fun();
    }
    
  • This is one of the use of Static member function.

Sagar Patel
  • 864
  • 1
  • 11
  • 22
1

A static member function is just a regular free function with a funny name. A pointer to a function is compatible with a pointer to a static member function.

A non-static member function is instead a function that receives and extra implicit hidden this parameter as first parameter. However a pointer to a non-static member function is not compatible with a pointer to a function.

To call a non-static member function using a pointer to member function you need to provide an instance... and also the syntax is quite strange: (x.*member_ptr)(...) if x is an object reference, or (x->*member_ptr)(...) if x is a pointer.

A pointer to a non-static member function and a pointer to a function are incompatible types and there's no portable way to convert one in the other. If you know the instance and you want to have a callable object to invoke one of its non-member function you can use (with C++11) an std::function wrapping a lambda.

#include <functional>
#include <string.h>
#include <iostream>

struct Foo {
    int x;
    Foo(int x) : x(x) {}
    void bar() { std::cout << x << std::endl; }
    static void baz() { std::cout << "Here\n"; }
};

int main(int argc, const char *argv[]) {

    void (Foo::*f)() = &Foo::bar; // Non-static function member pointer
    void (*g)() = &Foo::baz;      // Static member function = function

    Foo x(42);
    (x.*f)(); // Prints 42
    g();      // Prints "Here"

    // Portable way to call a non-static member function just using ff()
    std::function<void()> ff = [&x](){ x.bar(); };
    ff(); // Prints 42 too

    // Hack zone... just to show that on many compilers
    // even a pointer to non-static member function is just
    // a pointer to a function that accepts however `this`
    // as an extra first parameter.
    void (*hack)(void *);
    memcpy(&hack, &f, sizeof(hack));
    hack(&x); // Prints 42 too on g++/clang++ (NOT PORTABLE!!)

    return 0;
}
6502
  • 112,025
  • 15
  • 165
  • 265
  • So does the existence of the hidden this actually imply that a non static member function must always be in the context of an object, while a static member function must always exist outside the context of an object? If so then why declare it inside a class. And also this hidden this parameter, is it similar to the self parameter in python methods? – Satrapes Aug 24 '15 at 16:41
  • @Satrapes: yes a non-static member function can only be executed on an object instance. Technically the standard doesn't mandate the exact implementation but all compilers I know just generate the very same machine code that a free function would have with an extra parameter at the beginning of type pointer to instance (the `this` parameter). A static member function is instead a free function that lives in the namespace of the class and cannot access any non-static member because it doesn't have a `this`. – 6502 Aug 24 '15 at 16:48
  • Non-static member functions can change members of a instantiation of a class, so they must be in context of an object. Static member functions are not allowed to change a member of the class, therefore they can exist outside the context of an object. Declaring it inside of a class just makes sense, when the function is closely related to that class (like when you use a singleton pattern and have a static function to receive the one existing object). They could as well be defined outside of the class. – PeterO Aug 24 '15 at 16:50
  • @Satrapes: see example for what I mean with the extra `this` parameter... on g++ and clang++ the code prints 42 on the last call too – 6502 Aug 24 '15 at 16:57