<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Methoden Callbacks]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich schreibe mir ein kleines UI System und will jetzt irgendwie für UI Controls Methoden registrieren lassen.<br />
Das Design sollte in etwa so aussehen:</p>
<pre><code class="language-cpp">MyClass::onMouseClick(MouseEvent&amp; e) {
   cout &lt;&lt; &quot;Clicked&quot;;
}

MyClass::method() {
   Button* b = new Button;
   b-&gt;registerClickHandler(this, MyClass::onMouseClick);
}
</code></pre>
<p>Wie könnte man mit C++ eine Methode wie registerClickHandler() implementieren?<br />
Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/310020/methoden-callbacks</link><generator>RSS for Node</generator><lastBuildDate>Wed, 05 Aug 2026 06:14:11 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/310020.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 01 Nov 2012 18:43:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Methoden Callbacks on Thu, 01 Nov 2012 18:43:49 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich schreibe mir ein kleines UI System und will jetzt irgendwie für UI Controls Methoden registrieren lassen.<br />
Das Design sollte in etwa so aussehen:</p>
<pre><code class="language-cpp">MyClass::onMouseClick(MouseEvent&amp; e) {
   cout &lt;&lt; &quot;Clicked&quot;;
}

MyClass::method() {
   Button* b = new Button;
   b-&gt;registerClickHandler(this, MyClass::onMouseClick);
}
</code></pre>
<p>Wie könnte man mit C++ eine Methode wie registerClickHandler() implementieren?<br />
Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2266332</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2266332</guid><dc:creator><![CDATA[Wichtel]]></dc:creator><pubDate>Thu, 01 Nov 2012 18:43:49 GMT</pubDate></item><item><title><![CDATA[Reply to Methoden Callbacks on Thu, 01 Nov 2012 18:52:41 GMT]]></title><description><![CDATA[<p>Ich würde mit C++11 std::function benutzen:<br />
<a href="http://en.cppreference.com/w/cpp/utility/functional/function" rel="nofollow">http://en.cppreference.com/w/cpp/utility/functional/function</a></p>
<p>Für ältere Compiler gibt's das Gleiche bei boost.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2266339</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2266339</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Thu, 01 Nov 2012 18:52:41 GMT</pubDate></item><item><title><![CDATA[Reply to Methoden Callbacks on Thu, 01 Nov 2012 23:10:28 GMT]]></title><description><![CDATA[<p>Hier war schonmal eine ziemlich ähnliche Frage, die ich ausführlich beantwortet habe:<br />
<a href="http://www.c-plusplus.net/forum/p2167812#2167812" rel="nofollow">http://www.c-plusplus.net/forum/p2167812#2167812</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2266441</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2266441</guid><dc:creator><![CDATA[XSpille]]></dc:creator><pubDate>Thu, 01 Nov 2012 23:10:28 GMT</pubDate></item><item><title><![CDATA[Reply to Methoden Callbacks on Thu, 01 Nov 2012 23:23:15 GMT]]></title><description><![CDATA[<p>afaik arbeitet std::function mit dynamische alloziertem speicher, was die sache nicht gerade performant macht</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2266442</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2266442</guid><dc:creator><![CDATA[bhatty]]></dc:creator><pubDate>Thu, 01 Nov 2012 23:23:15 GMT</pubDate></item><item><title><![CDATA[Reply to Methoden Callbacks on Fri, 02 Nov 2012 00:14:28 GMT]]></title><description><![CDATA[<p>bhatty schrieb:</p>
<blockquote>
<p>afaik arbeitet std::function mit dynamische alloziertem speicher, was die sache nicht gerade performant macht</p>
</blockquote>
<p>Solange du keine gemeinsame Basisklasse für alle möglichen Objekte hast, wirst du wohl nicht ohne dynamisch alloziiertem Speicher auskommen.<br />
Außerdem finde ich Performance-Bedenken bei Listenern schon ziemlich übertrieben.<br />
So viel langsamer ist es nämlich auch nicht.</p>
<p>Auch wenn der folgende Code bei mir das gewünschte Ergebnis liefert, habe ich starke Bedenken, dass er standard-konform ist:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt; 
#include &lt;vector&gt;

struct Dummy{
};

struct Foo{
	void print_i(){
		std::cout &lt;&lt; &quot;i=&quot; &lt;&lt; i &lt;&lt; std::endl;
	}

	int i;
};

struct Bar{
	void print_j(){
		std::cout &lt;&lt; &quot;j=&quot; &lt;&lt; j &lt;&lt; std::endl;
	}

	int j;
};

int main(){
	Foo foo;
	foo.i = 3;
	Bar bar;
	bar.j = 5;
	typedef void (Dummy::*dummy_ptr_type)();
	std::vector&lt;std::pair&lt;Dummy*, dummy_ptr_type&gt; &gt; listeners;
	listeners.push_back(std::make_pair(reinterpret_cast&lt;Dummy*&gt;(&amp;foo), reinterpret_cast&lt;dummy_ptr_type&gt;(&amp;Foo::print_i)));
	listeners.push_back(std::make_pair(reinterpret_cast&lt;Dummy*&gt;(&amp;bar), reinterpret_cast&lt;dummy_ptr_type&gt;(&amp;Bar::print_j)));
	for(std::vector&lt;std::pair&lt;Dummy*, dummy_ptr_type&gt; &gt;::iterator it = listeners.begin(); it!=listeners.end(); ++it){
		Dummy* obj = it-&gt;first;
		dummy_ptr_type ptr = it-&gt;second;
		(obj-&gt;*ptr)();
	}
}
</code></pre>
<blockquote>
<p>i=3<br />
j=5</p>
</blockquote>
<p>Gruß,<br />
XSpille</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2266449</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2266449</guid><dc:creator><![CDATA[XSpille]]></dc:creator><pubDate>Fri, 02 Nov 2012 00:14:28 GMT</pubDate></item><item><title><![CDATA[Reply to Methoden Callbacks on Fri, 02 Nov 2012 12:41:55 GMT]]></title><description><![CDATA[<p>bhatty schrieb:</p>
<blockquote>
<p>afaik arbeitet std::function mit dynamische alloziertem speicher, was die sache nicht gerade performant macht</p>
</blockquote>
<p>Jain. Eine gescheite Implementierung macht von der &quot;small function optimization&quot; gebrauch. Man kann von einer solchen Implementierung ruhig erwarten, kleine und trivial kopierbare Funktoren direkt im std::function&lt;&gt;-Objekt abzulegen, wie z.b. so etwas:</p>
<pre><code class="language-cpp">std::function&lt;void()&gt; f = [this]{this-&gt;foo();};
</code></pre>
<p>Da hat sich sogar mal Dave Abrahams intensiv mit beschäftigt. Er untersuchte, wieviel Overhead hier bei abfällt, wenn man std::function intelligent implementiert. Das Ergebnis: Der Overhead ist vernachlässigbar. Damit flog dann auch std::reference_closure&lt;&gt; aus dem Standard, was ursprünglich für Lambdas aus Performancegründen eingeführt wurde.</p>
<p>(Angaben ohne Gewähr. Ich habe die Dinge nicht extra nochmal verfolgt, sondern verlasse mich da auf meine Erinnerung)</p>
<p>Nachtrag:<br />
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2845.html" rel="nofollow">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2845.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2266564</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2266564</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Fri, 02 Nov 2012 12:41:55 GMT</pubDate></item></channel></rss>