#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    13
    Rep Power
    0

    Static function pointer


    Why cant I do the following:

    ..........................header file:
    class CFPointerTest{

    private:

    public:
    CFPointerTest();

    static int (*test)();

    static void setF();
    static int f1();

    };

    ..........................cpp file:
    CFPointerTest::CFPointerTest(){



    }

    void CFPointerTest::setF(){

    CFPointerTest::test= f1;

    }

    int CFPointerTest::f1(){

    return 1;

    }

    ........................................
    It says "undefined reference to `CFPointerTest::test'"
    It works when test and setF are not static.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    I'm not completely sure, but it must be because of the nature of static class members.

    In order to use (almost) any class member, you need to instantiate an object of that class, in which case the constructor should take care of initializing all pointer members. The exception to that rule is the static member, which does not depend on any object having been instantiated. In fact, you can still call and use a static class member even if absolutely no objects of that class exist. However, that also means that no constructor ever needs to have been called, which means that no pointers will have been initialized.

    It therefore seems impossible to have a static pointer member since it could never be initialized.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    For static function pointers (or for that matter, any kind of static member variable), you need to initialize them outside a method, like you're initializing a global variable:
    CFPointerTest.h
    Code:
    #ifndef CFPOINTER_TEST
    #define CFPOINTER_TEST
    
    class CFPointerTest{
    
     private:
    
     public:
        CFPointerTest();
    
        static int (*test)();
        static void setF();
        static int f1();
    };
    
    #endif
    CFPointerTest.cpp
    Code:
    #include "CFPointerTest.h"
    
    CFPointerTest::CFPointerTest() {
    }
    
    int (*CFPointerTest::test)() = f1;
    
    void CFPointerTest::setF() {
        //CPointerTest::test = f1;
    }
    
    int CFPointerTest::f1() {
        return 1;
    }
    That will make it work.

    You need to initialize like this because a static member variable is common to all instances of a class and should be initialized always before any instances of class are instantiated.
    Last edited by Scorpions4ever; September 8th, 2012 at 02:13 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    13
    Rep Power
    0
    Originally Posted by dwise1_aol
    ....However, that also means that no constructor ever needs to have been called, which means that no pointers will have been initialized.

    It therefore seems impossible to have a static pointer member since it could never be initialized.
    Sorry, I dont really understand. Im no expert. How do you mean initialized? It just holds a memory adress like an int holds a number. I can assign something to a static int member(through a static function) without the constructor being called. Why is it different?
    Thanks!


    EDIT:
    Sorry! I remember now, ...like with singletons.
    (like CKeys* CKeys::pInstance= 0; in the cpp)
    Thanks!
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by Aliii
    How do you mean initialized?
    The problem is not in fact one of initialisation, but rather one of instantiation. You have to create the static instance because it is independent of any single object instance.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by Aliii
    Sorry, I dont really understand. Im no expert. How do you mean initialized? It just holds a memory adress like an int holds a number. I can assign something to a static int member(through a static function) without the constructor being called. Why is it different?
    Thanks!
    It's for exactly the same reason as when you have a header file with
    Code:
    extern int x;
    you need a source file (or "compilation unit") that defines x. The header file only declares that x exists; it doesn't set aside any space for it. A static member of a class is just like that: the "static ..." declaration in the class body only declares that this member exists; it doesn't reserve any space for it (unlike a non-static member, which has space reserved for it in each instance of the class). When you define the static member in a source file and compile that file, the resulting object file will have the necessary bytes reserved for it in its data section.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    13
    Rep Power
    0
    Originally Posted by Scorpions4ever
    For static function pointers (or for that matter, any kind of static member variable), you need to initialize them outside a method, like you're initializing a global variable:
    CFPointerTest.h
    Code:
    #ifndef CFPOINTER_TEST
    #define CFPOINTER_TEST
    
    class CFPointerTest{
    
     private:
    
     public:
        CFPointerTest();
    
        static int (*test)();
        static void setF();
        static int f1();
    };
    
    #endif
    CFPointerTest.cpp
    Code:
    #include "CFPointerTest.h"
    
    CFPointerTest::CFPointerTest() {
    }
    
    int (*CFPointerTest::test)() = f1;
    
    void CFPointerTest::setF() {
        //CPointerTest::test = f1;
    }
    
    int CFPointerTest::f1() {
        return 1;
    }
    That will make it work.

    You need to initialize like this because a static member variable is common to all instances of a class and should be initialized always before any instances of class are instantiated.
    Thanks! It works now.
    I just dont understand that syntax.
    Why this:
    int (*CFPointerTest::test)() = f1;

    And why not this?
    int CFPointerTest::((*test)())= f1;

    Comments on this post

    • dwise1_aol disagrees : [b]Disable smilies![/b] They serve no purpose and they almost always completely frak up C++ code!

IMN logo majestic logo threadwatch logo seochat tools logo