Thread: Pointer to function in C++

  1. #1
    Cgangster
    Guest

    Pointer to function in C++

    Hi,
    Can anyone help me how to creat a pointer function in C++?

    Code:
    class Mailbox
    {
        public:
           Mailbox();
           Mailbox(int add, string mess);
           void setMessage(string mess);
           void setAddress(int add); 
           char * getMessage();
           void (*fun)(int callback); 
    
           ~Mailbox();
    };
    How to implement and pass the pointer function in a class?
    Thank you.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How to implement and pass the pointer function in a class?
    It depends on what you're trying to do with this function pointer, in most cases I would have the address of the function be passed either to the constructor as an explicit argument, or assigned with a member function to a private function pointer which the class can then use:
    Code:
    #include <iostream>
    
    typedef std::ostream& (*pf)( std::ostream&, const char * );
    
    class test
    {
    public:
      explicit test ( pf func ) : display ( func ) {}
    
      void print_console ( const char *mess ) { display ( std::cout, mess ); }
    private:
      pf display;
    };
    
    std::ostream& display_mess ( std::ostream& out, const char *mess )
    {
      return out<< mess;
    }
    
    int main()
    {
      test t ( display_mess );
    
      t.print_console ( "This is a test" );
    
      std::cin.get();
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Cgangster
    Guest
    Thanks a lot! Prelude.
    If you dont mind, can you illustrate a less complicate example with
    not involved with C++ buildin objects like ostream.
    Again, thanks for your help
    Cgangster

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >If you dont mind, can you illustrate a less complicate example
    No problem, I wasn't sure of your experience level. This is simplified a bit and commented liberally with details I thought may not make sense at first, let me know if you would like me to explain something that is unclear.
    Code:
    #include <iostream>
    
    typedef void (*pf)( const char * );
    // Now pointers to functions of this type can be declared as
    //   pf <variable>
    //
    //   instead of the ugly and confusing
    //
    //   void (*<variable>)( const char * );
    
    class test
    {
    public:
      explicit test ( pf func ) { display = func; }
      // Declaring a single argument constructor as explicit means
      // that it can only be called as constr <object> ( <argument> );
      // and not as constr <object> = <argument>;
    
      void print_console ( const char *mess ) { display ( mess ); }
    private:
      pf display; // Pointer to function
    };
    
    void display_mess ( const char *mess )
    {
      std::cout<< mess;
    }
    
    int main()
    {
      test t ( display_mess );
      // You can pass the address of a function as either <name> or &<name>.
      // Both forms are equivalent since any operation performed on a function
      // is either calling it or taking its address. Since you obviously aren't
      // calling it, then you must be taking its address.
    
      t.print_console ( "This is a test" );
    
      std::cin.get();
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM