DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Callbacks

  1. #1
    Join Date
    Dec 2003
    Posts
    3,366

    Callbacks

    How are callbacks generally done?

    I handled mine this crude way:
    I made a "meta object" with pointers to specific objects (similar objects that access different hardware and handle some things differently). The base class that all of them share (the common stuff) uses the meta object (passed in from the parent) (which calls the parent function using the only pointer in the meta object that is not null). It works but I'm sure that its not the best way to do things. The only examples I have seen are also crude, filled with function pointers and general hackery that I do not like the look of.

    Any comments / old articles ?

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Normally, you use pointers to member functions in such cases. Here's a tutorial on pointers to members:
    http://www.informit.com/guides/conte...lus&seqNum=142
    You can also use function objects (also called functors). They offer several adavantages over bare pointers to members such as being able to store the sate of the previous call, minimizing the number of arguments that need to be passed (intead, they are stored as data members) and they can automatically cleanup after themselves:
    http://www.devx.com/DevX/LegacyLink/9471
    Finally, there are plain old C function pointers that you can use, but only if you are forced to...
    Danny Kalev

  3. #3
    Join Date
    Jul 2005
    Posts
    26
    I don't know whether I understood you correctly, but here's my suggestion: Create kind-of "interfaces":
    Code:
    class AI
    {
    public:
      virtual void AI_Output(int i) = 0;
    };
    
    class A
    {
    public:
      void DoTheWork(AI * pAI)
      {
        for (int i=0; i<100; ++i)
        {
          pAI->AI_Output(i);
        }
      };
    };
    
    class U : public AI // implements the interface AI, may also inherit multiple base classes
    {
    public:
      void AI_Output(int i)
      {
        printf("%i\n",i);
      };
    
      void Start()
      {
        A a;
        a.DoTheWork(static_cast<AI*>(this));
      };
    
    /* Do much other stuff here */
    };
    This code example is untested, never compiled it. Hope it works ;)

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by jonnin
    How are callbacks generally done?

    I handled mine this crude way:
    I made a "meta object" with pointers to specific objects (similar objects that access different hardware and handle some things differently). The base class that all of them share (the common stuff) uses the meta object (passed in from the parent) (which calls the parent function using the only pointer in the meta object that is not null). It works but I'm sure that its not the best way to do things. The only examples I have seen are also crude, filled with function pointers and general hackery that I do not like the look of.

    Any comments / old articles ?
    I don't think I can visualize this class hierarchy example. What is a "meta class" exactly? And what exactly is the hierarchy? Are there many derived classes and a single parent or are there several intermediate derived classes?
    There are probably two or three design patterns that you can use to simplify the design, such as Observer, Visitor and perhaps the Template pattern (whcih has nothing to do with C++ templates).
    Danny Kalev

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    I could not figure out a way to do what I want with callbacks (I suppose, what I really want is a callforward).

    This is what I finally came up with; it would be better if the child class constructor fed its pointers, which I will probably do when I move the idea into our design.

    It lets the child call the parent function without the mess I had before. This does what I want, is there a name for it or a different, better way to go about it?

    #include<iostream>
    using namespace std;

    template<class type> class child
    {
    public:
    child();
    void print();
    type * me;
    };

    template<class type> child<type>::child()
    {
    me = NULL;
    }

    template<class type> void child<type>::print()
    {
    cout << "child ";
    me->print1(); //parent must have this function!
    }


    class p1 : public child<p1>
    {
    public:
    p1();
    void print1();
    int i;
    };

    p1::p1()
    {
    //should be fed to child via constructor
    me = this;
    }

    void p1::print1()
    {
    cout << " P1\n" << endl;
    }


    class p2 : public child<p2>
    {
    public:
    p2();
    void print1();
    int i;
    };

    p2::p2()
    {
    me = this;
    }

    void p2::print1()
    {
    cout << " P2\n" << endl;
    }


    int main()
    {
    p1 test1;
    p2 test2;

    test1.print(); //child p1 printed
    test2.print(); //child p2 printed

    return 0;

    }
    Last edited by jonnin; 07-29-2005 at 11:32 AM.

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    There are a few design patterns that can simplify this task,such as Command and Template. However, if your code works fine, I don't think that adding any of these pattersn will necessary improve readability maintenance etc.
    Danny Kalev

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    Ty for that. It seems to work but didnt like passing this down the constructor pipe (warning: top object construction not complete). Since it works Im going to do it anyway of course ;)

Similar Threads

  1. Callbacks from 3rd party DLL
    By Howard in forum .NET
    Replies: 1
    Last Post: 12-06-2002, 08:12 AM
  2. Re: Audio Mixers and Callbacks
    By Russ Suter in forum VB Classic
    Replies: 1
    Last Post: 04-17-2001, 06:32 PM
  3. Replies: 2
    Last Post: 08-01-2000, 10:19 AM
  4. Replies: 2
    Last Post: 08-01-2000, 10:19 AM
  5. Replies: 1
    Last Post: 05-11-2000, 05:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center