 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Olaf Petzold Guest
|
Posted: Mon Sep 15, 2003 1:23 pm Post subject: STL Algorithms vs. Hand-Written Loops/S.Meyers |
|
|
Hallo,
derzeit probiere ich mich mit der typed message und wollte obigen Artikel
anwenden:
template<class T>
class Event {
public:
class Handler
{
public:
Handler() { Event<T>::attach(this); }
virtual ~Handler() { Event<T>::detach(this); }
virtual int handleEvent(const T&) = 0;
};
public:
typedef std::list<Handler*> list_type;
virtual ~Event() { }
static void attach(Handler* h) { m_registry.push_back(h); }
static void detach(Handler* h) { m_registry.remove(h); }
static void notify(Event<T>* e) {
#if 1 // mein Problem
std::for_each(m_registry.begin(), m_registry.end(),
std::mem_fun_ref(&Handler::handleEvent(*static_cast<T*>(e))));
#else
typedef typename list_type::iterator iterator;
for(iterator i = m_registry.begin(); i != m_registry.end(); ++i) {
(*i)->handleEvent(*static_cast<T*>(e));
}
#endif
}
void notify() { T::notify(this); }
private:
static list_type m_registry;
};
class SaveConfigEvent : public Event<SaveConfigEvent> { };
Unglücklicherweise hat meine Member function notify ein Argument, so daß es
nicht so recht klappt:
In static member function `static void Event<T>::notify(Event<T>*) [with T =
ReadConfigEvent]':
[...]
cannot call member function `int Event<T>::Handler::handleEvent(const T&)
[with T = SaveConfigEvent]' without object
So recht nachvollziehen kann ich es nicht. Geht es überhaupt so?
Vielen Dank
Olaf
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Markus Breuer Guest
|
Posted: Mon Sep 15, 2003 1:48 pm Post subject: Re: STL Algorithms vs. Hand-Written Loops/S.Meyers |
|
|
Olaf Petzold schrieb:
| Quote: | Hallo,
derzeit probiere ich mich mit der typed message und wollte obigen Artikel
anwenden:
template<class T
class Event {
public:
class Handler
{
public:
Handler() { Event
virtual ~Handler() { Event<T>::detach(this); }
virtual int handleEvent(const T&) = 0;
};
public:
typedef std::list<Handler*> list_type;
virtual ~Event() { }
static void attach(Handler* h) { m_registry.push_back(h); }
static void detach(Handler* h) { m_registry.remove(h); }
static void notify(Event<T>* e) {
#if 1 // mein Problem
std::for_each(m_registry.begin(), m_registry.end(),
std::mem_fun_ref(&Handler::handleEvent(*static_cast<T*>(e))));
#else
typedef typename list_type::iterator iterator;
for(iterator i = m_registry.begin(); i != m_registry.end(); ++i) {
(*i)->handleEvent(*static_cast<T*>(e));
}
#endif
}
void notify() { T::notify(this); }
private:
static list_type m_registry;
};
class SaveConfigEvent : public Event<SaveConfigEvent> { };
Unglücklicherweise hat meine Member function notify ein Argument, so daß es
nicht so recht klappt:
In static member function `static void Event<T>::notify(Event<T>*) [with T =
ReadConfigEvent]':
[...]
cannot call member function `int Event<T>::Handler::handleEvent(const T&)
[with T = SaveConfigEvent]' without object
So recht nachvollziehen kann ich es nicht. Geht es überhaupt so?
|
Du verlangst von deinem Konstrukt eine Member Funktion ohne this-Zeiger
aufzurufen. Wenn handleEvent eine static Member-Funktion ist, würde es
so funktionieren.
Du kannst aber einfach Abhilfe schaffen, indem du einen Funktionsadapter
verwendest:
std::mem_fun( &Handler::eventHandler )
erzeugt dir den Adapater, den du mit ...(this,e) instanzierst. Um den
zweiten Parameter zu binden, verwendest du zusätzlich
std::bind2nd( std::mem_fun(...), e )
diesen Ausdruck kannst du mit ...(this) aufrufen. Egebnis:
std::for_each( ..., std::bind2nd( std::mem_fun(...) ) );
Gruß Markus
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Joerg Barfurth Guest
|
Posted: Mon Sep 15, 2003 10:13 pm Post subject: Re: STL Algorithms vs. Hand-Written Loops/S.Meyers |
|
|
Olaf Petzold <opetzold (AT) wit (DOT) regiocom.net> wrote:
| Quote: | derzeit probiere ich mich mit der typed message und wollte obigen Artikel
anwenden:
template<class T
class Event {
public:
class Handler
{
public:
Handler() { Event
virtual ~Handler() { Event<T>::detach(this); }
virtual int handleEvent(const T&) = 0;
};
public:
typedef std::list<Handler*> list_type;
virtual ~Event() { }
static void attach(Handler* h) { m_registry.push_back(h); }
static void detach(Handler* h) { m_registry.remove(h); }
static void notify(Event<T>* e) {
#if 1 // mein Problem
std::for_each(m_registry.begin(), m_registry.end(),
std::mem_fun_ref(&Handler::handleEvent(*static_cast<T*>(e))));
|
std::mem_fun (warum eigentlich ..._ref? deine list enthält doch Zeiger!)
nimmt als Argument einen Memberfunktionszeiger. Du versuchst aber die
Adresse des Resultats eines Funktionsaufrufes zurückzugeben. Dieser
Aufruf [Handler::handleEvent(*static_cast<T*>(e))] versucht eine
Memberfunktion ohne Objekt aufzurufen - geht nicht. Und wenn es ginge
würdest du versuchen die Adresse eines rvalue (vom Typ int) zu bestimmen
- geht auch nicht. Und wenn es ginge, würdest du den erhaltenen int* an
std::mem_fun_ref(...) übergeben - geht nochmal nicht.
Überprüfe nochmal deine Klammerung, lies noch mal nach was mem_fun(_ref)
bei einer einargumentigen Memberfunktion liefert, überlege dir ob du
hier mit std::bind2nd weiter kommst - und, zu guter Letzt, tue dir und
allen die den Code warten sollen den Gefallen diesen Loop von Hand
auszuprogrammieren :-o
| Quote: | #else
typedef typename list_type::iterator iterator;
for(iterator i = m_registry.begin(); i != m_registry.end(); ++i) {
(*i)->handleEvent(*static_cast<T*>(e));
}
#endif
|
Ciao, Jörg
--
Jörg Barfurth [email]barfurth (AT) gmx (DOT) net[/email]
<<<<<<<<<<<<< using std::disclaimer; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
Software Developer http://util.openoffice.org
StarOffice Configuration # Deutsch:http://de.openoffice.org
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|