Re: The "widget" problem
Thomas Flynn <tflynn@gc.cuny.edu> writes:
What do you mean by this? I think that using member fns for the
callbacks has certain advantages in this situation. One of them being
that it also allows the user inherit from the button and modify it if
they wanted to. This example deals with the situation where a widget
contains a button, but when a widget is a button, modified in some way,
then inheriting from it and overloading press() or some other
function altogether would be more appropriate.
I am not sure whether I understand this advantage.
To make it clear what I was thinking about, I wrote the
following code.
I do not see how passing the object instead of a member
function inhibits to inherit from a button and modify it:
#include <iostream> // ::std::cout
#include <ostream> // <<
#include <vector> // ::std::vector
struct press_listener
{ virtual void pressed() = 0; };
struct button : public press_listener
{ virtual void pressed(){ ::std::cout << "pressed" << '\n'; }};
struct framework
{ ::std::vector< press_listener* >observer;
void accept( press_listener * const listener )
{ observer.push_back( listener ); }
void run()
{ for( press_listener * const listener : observer )
listener->pressed(); }};
int main()
{ framework f;
button b;
f.accept( &b ); /* a pointer to an object is passed here
instead of a pointer to a member function */
f.run(); }