Re: [gtkmm] General C++ Design Question: relation of GUI toapplication



>> class Foo {
>>       ...
>>       gint callback ();
>>
>>       static gint _callback (void *arg) {
>>           static_cast<Foo *>(arg)->callback ();
>>       }
>> };
>
>Let me make sure I understand that:  Say we have a class Bar which contains a
>member ,barsFooClass, that is pointer-to class Foo.

   [ ... ]

You've made it one or more steps more complex than necessary.

class Foo {

    gint some_value;

public:
    Foo (gint i) : some_value (i) {}
	
    gint  fooFunction(int x) {
         return x + some_value;
    }

 };


 class Bar {

     Foo& barsFooClass; /* could be static, but doesn't need to be */
     
  public:
     Bar (Foo& f) : barsFooClass (f) {}
     
     static int _barFunction(void *arg) {
	    return reinterpret_cast<Bar *>(arg)->barFunction ();
     }

     int barFunction ();
            return(barsFooClass.fooFunction(8) );
     }

  };


  int main() {

         Foo fooclass (7);
         Bar barclass (fooclass);

	 /* every 1 second, call Foo::fooFunction() on
	    whatever Foo barclass references.

	    Note that the timeout method doesn't allow you
	    to supply arguments unless they are implicitly
	    passed in the third argument. Ask if you
	    don't understand this point.
	  */

         gtk_timeout_add(1000, Bar::_barFunction, &barclass);
  }

note the use of references to avoid the possibility of null pointers,
and the more correct use of reinterpret_cast<> instead of static_cast<>.

--p






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]