How to intialize pointer to member function inside ctor

This is a discussion on How to intialize pointer to member function inside ctor within the C++ Programming forums, part of the General Programming Boards category; How to intialize pointer to member function inside constructor. Code: class Foo{ private: void (Foo::*func)(); void _func(){} public: Foo(){ func ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    10

    How to intialize pointer to member function inside ctor

    How to intialize pointer to member function inside constructor.
    Code:
    class Foo{
    private:
      void (Foo::*func)();
      void _func(){}
    public:
      Foo(){
        func = &Foo::_func;
        // _func = 0x0, func = someaddress
        printf("func: 0x%08X, _func: 0x0%08X", func, &Foo::_func); 
      }
    };

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    If you actually want to initialise it, the constructor should be defined as:
    Code:
    Foo() : func(&Foo::_func) {}
    As for printing the member function pointer: the %X format specifier is not appropriate. The %p format specifier might be appropriate with a cast to void*, but if I remember correctly it is not guaranteed that there is a valid conversion from a pointer to a function or member function to void*. You may be better off using formatted output with a std::ostream like std::cout instead.

    EDIT:
    By the way, why do you want to do this in the first place?
    Last edited by laserlight; 10-21-2009 at 01:38 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    I think this code can make you understand what you want

    Code:
    #include <stdio.h>
    
    class Test {
    private:
      int (Test::*m_FuncPtr)(const int, const int);
    
    public:
      Test() : m_FuncPtr(NULL) {}
    
      Test(int (Test::*FuncPtr)(const int, const int)) :
           m_FuncPtr(FuncPtr) {}
    
      int Sum(const int a, const int b) { return a + b; }
    };
    
    int main() {
      Test obj(&Test::Sum);
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a class inside the WindowsProcedure function
    By like_no_other in forum Windows Programming
    Replies: 3
    Last Post: 06-01-2009, 12:52 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21