CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2008
    Posts
    13

    Question member static function pointers

    when i call a member static function pointer which is initialized in constructor. it gives me unresolved external linking error.

    any ideas?
    thanks already

  2. #2
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: member static function pointers

    Static members cannot be initialized inside the class (save the few exceptions).
    Meaning, it can only be initialized at definition(allocation).
    Move your statement outside the class body.

  3. #3
    Join Date
    Jun 2008
    Posts
    13

    Re: member static function pointers

    let me give an example;

    //class.h

    class a
    {
    public :
    a();

    static int x;
    static int (*y)(int z);
    };

    //class.cpp

    int a::x = 0; /*this lets me to use x from everywhere*/
    int (a::*y) = 0; /*when i try to reach y. now it gives unresolved external error*/


    maybe i just need syntax to set function pointer some value.
    Last edited by phenixa; May 8th, 2009 at 08:11 AM.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: member static function pointers

    Honestly I find function pointer syntax to be a pain and unintuitive....so whenever I need to use one, the first thing I do is typedef it. Once you do that then the definition would look like
    Code:
    MyFunPtr a::myPtr = NULL;

  5. #5
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: member static function pointers

    Quote Originally Posted by phenixa View Post
    let me give an example;

    //class.h

    class a
    {
    public :
    a(){}

    static int x;
    static int (*y)(int z);
    };

    //class.cpp

    int a::x = 0; /*this lets me to use x from everywhere*/
    int (a::*y) = 0; /*when i try to reach y. now it gives unresolved external error*/


    maybe i just need syntax to set function pointer some value.
    Ah, you said you initialized it in the class constructor! did you not!!
    And Lindley is right. Using typedef is much easier~

  6. #6
    Join Date
    Jun 2008
    Posts
    13

    Re: member static function pointers

    thank you very much lindley

  7. #7
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: member static function pointers

    Quote Originally Posted by phenixa View Post
    thank you very much lindley
    Just for the record, the problem was the class, not your syntax.
    And you're welcome.

  8. #8
    Join Date
    Jun 2008
    Posts
    13

    Re: member static function pointers

    Quote Originally Posted by potatoCode View Post
    Ah, you said you initialized it in the class constructor! did you not!!
    And Lindley is right. Using typedef is much easier~
    i meant that yeah

    like this

    //class.cpp

    a::a()
    {
    y = some function address;

    }

    thanks for helping

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured