Pointer to function to handle event.Is a good idea?

This is a discussion on Pointer to function to handle event.Is a good idea? within the C++ Programming forums, part of the General Programming Boards category; i'm using pointer to function inside my class to handle event. For example: Code: #include <iostream> using namespace std; // ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    2

    Pointer to function to handle event.Is a good idea?

    i'm using pointer to function inside my class to handle event.
    For example:

    Code:
    #include <iostream>
    
    using namespace std;
    
    // MY_CLASS
    
    class test 
    {     
          private:
                  string value;        
          public:
                  void SetValue(string source) 
                         {  if (OnValueChange!=NULL)(*OnValueChange)(value,source);
                            value = source;}    
                  string GetValue() { return value;}
                  void (*OnValueChange)(string OldValue,string NewValue);    
          };
    
    // END_MY_CLASS
    
    //MAIN PROGRAM
    void handleOnValueChange(string OldValue,string NewValue)
    {
         cout<<"OLD VALUE : " << OldValue << "\n";
         cout<<"NEW VALUE : " << NewValue << "\n";
         cout<<"-----------------------------\n";
     }    
    
    int main(int argc, char *argv[])
    {
        test *atest ;
        atest = new test;
        atest->OnValueChange = handleOnValueChange;
        atest->SetValue("TEST");
        atest->SetValue("ANOTHER TEST");
        delete atest;
        system("PAUSE");
        return 0;
    }
    
    //END MAIN PROGRAM
    This is a good idea or is better using virtual function,inheritance ??

    thanx,

    Gianni

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    your program does not ask operating member data of test in the handleOnValueChange. so it is enough

    using a functor instead of a pointer to function is a better way, it is more flexible.

    using virtual function is another way to fix this problem, but is it designed overly?

    there are many ways to fix the problem, be sure you thought it enough before using it
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    I'm curious as to what his code is doing. What is [OnValueChange] ?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You could use Boost.Signals to implement a flexible system of signals and slots.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 03:53 PM

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