<?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[Map mit std::function]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich schreibe alten C++ mit vielen Funktionpointer um auf C++11. Jetzt habe ich das Problem Funktionpointer von Memberfunktionen in einer Map zu speichern.</p>
<p>Hier meine bisherige Lösung</p>
<pre><code>#include &lt;functional&gt;
#include &lt;iostream&gt;
#include &lt;map&gt;

using namespace std;

struct Para
{
	int value;
};

class Base
{

};

class Derived1 : public Base
{
public:
	void command11(Para para) { cout &lt;&lt; &quot;Command 11: &quot; &lt;&lt; para.value &lt;&lt; endl; }
	void command12(Para para) { cout &lt;&lt; &quot;Command 12: &quot; &lt;&lt; para.value &lt;&lt; endl; }
};

class Derived2 : public Base
{
public:
	void command21(Para para) { cout &lt;&lt; &quot;Command 21: &quot; &lt;&lt; para.value &lt;&lt; endl; }
	void command22(Para para) { cout &lt;&lt; &quot;Command 22: &quot; &lt;&lt; para.value &lt;&lt; endl; }
};

int main()
{
	map&lt;int, std::function&lt;void(Para)&gt;&gt; m;

	m[11] = [=](Para p) {Derived1 d; d.command11(p);};
	m[12] = [=](Para p) {Derived1 d; d.command12(p);};
	m[21] = [=](Para p) {Derived2 d; d.command21(p);};
	m[22] = [=](Para p) {Derived2 d; d.command22(p);};

	int code = 21;

	auto it = m.find(code);
	if (it != end(m))
		it-&gt;second({ 5 });

	cin.get();
	return 0;
}
</code></pre>
<p>Mich stören die Zeilen mit</p>
<pre><code>m[11] = [=](Para p) {Derived1 d; d.command11(p);};
</code></pre>
<p>. Kann man das eleganter machen, z.B. mit einer Funktion, die ich die Adresse der Klasse und die Adresse der Funktion angebe?<br />
Sowas wie</p>
<pre><code>m[11] = makeFunction(&amp;Derived1(), &amp;Derived1::command11);
</code></pre>
<p>?</p>
<p>Dieser Code soll nur mein Problem verdeutlichen. Die Klassen werden später viel komplexer sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/339252/map-mit-std-function</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 07:33:22 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/339252.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 15 Aug 2016 17:55:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Map mit std::function on Mon, 15 Aug 2016 17:55:12 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich schreibe alten C++ mit vielen Funktionpointer um auf C++11. Jetzt habe ich das Problem Funktionpointer von Memberfunktionen in einer Map zu speichern.</p>
<p>Hier meine bisherige Lösung</p>
<pre><code>#include &lt;functional&gt;
#include &lt;iostream&gt;
#include &lt;map&gt;

using namespace std;

struct Para
{
	int value;
};

class Base
{

};

class Derived1 : public Base
{
public:
	void command11(Para para) { cout &lt;&lt; &quot;Command 11: &quot; &lt;&lt; para.value &lt;&lt; endl; }
	void command12(Para para) { cout &lt;&lt; &quot;Command 12: &quot; &lt;&lt; para.value &lt;&lt; endl; }
};

class Derived2 : public Base
{
public:
	void command21(Para para) { cout &lt;&lt; &quot;Command 21: &quot; &lt;&lt; para.value &lt;&lt; endl; }
	void command22(Para para) { cout &lt;&lt; &quot;Command 22: &quot; &lt;&lt; para.value &lt;&lt; endl; }
};

int main()
{
	map&lt;int, std::function&lt;void(Para)&gt;&gt; m;

	m[11] = [=](Para p) {Derived1 d; d.command11(p);};
	m[12] = [=](Para p) {Derived1 d; d.command12(p);};
	m[21] = [=](Para p) {Derived2 d; d.command21(p);};
	m[22] = [=](Para p) {Derived2 d; d.command22(p);};

	int code = 21;

	auto it = m.find(code);
	if (it != end(m))
		it-&gt;second({ 5 });

	cin.get();
	return 0;
}
</code></pre>
<p>Mich stören die Zeilen mit</p>
<pre><code>m[11] = [=](Para p) {Derived1 d; d.command11(p);};
</code></pre>
<p>. Kann man das eleganter machen, z.B. mit einer Funktion, die ich die Adresse der Klasse und die Adresse der Funktion angebe?<br />
Sowas wie</p>
<pre><code>m[11] = makeFunction(&amp;Derived1(), &amp;Derived1::command11);
</code></pre>
<p>?</p>
<p>Dieser Code soll nur mein Problem verdeutlichen. Die Klassen werden später viel komplexer sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2505582</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2505582</guid><dc:creator><![CDATA[ErnstH]]></dc:creator><pubDate>Mon, 15 Aug 2016 17:55:12 GMT</pubDate></item><item><title><![CDATA[Reply to Map mit std::function on Mon, 15 Aug 2016 18:12:48 GMT]]></title><description><![CDATA[<p>Was du da zeigst braucht keine Klassen/Objekte, da du nirgends den Objekzustand berücksichtigst. Das könnte man mit einfachen Funktionen lösen. Solltest du das Beispiel zu sehr vereinfacht haben, wirst du ja irgendwann den geänderten Zustand benutzen wollen, d.h. irgenwie mussst du dir die Objekte merken. Davon hängt alles weitere ab.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2505588</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2505588</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Mon, 15 Aug 2016 18:12:48 GMT</pubDate></item><item><title><![CDATA[Reply to Map mit std::function on Mon, 15 Aug 2016 19:03:35 GMT]]></title><description><![CDATA[<p>Innerhalb der verschiedenen Commands wird auf gemeinsame Daten innerhalb der Klasse zu gegriffen z.B. (wieder total vereinfacht)</p>
<pre><code>class Base
{

};

class Derived1 : public Base
{
private:
	int value;

public:
	void command11(Para para) { value += para.value; cout &lt;&lt; &quot;Command 11: &quot; &lt;&lt; para.value &lt;&lt; endl; }
	void command12(Para para) { value -= para.value; cout &lt;&lt; &quot;Command 12: &quot; &lt;&lt; para.value &lt;&lt; endl; }
};
</code></pre>
<p>So dass das Auslagern der Commands in Funktionen nicht möglich ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2505600</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2505600</guid><dc:creator><![CDATA[ErnstH]]></dc:creator><pubDate>Mon, 15 Aug 2016 19:03:35 GMT</pubDate></item><item><title><![CDATA[Reply to Map mit std::function on Mon, 15 Aug 2016 19:18:19 GMT]]></title><description><![CDATA[<p>Dann muss es doch etwas in der Art sein</p>
<pre><code>Derived1 d1;
    m[11] = [&amp;](Para p) { d1.command11(p);}; 
    m[12] = [&amp;](Para p) { d1.command12(p);}; 
    Derived2 d2;
    m[21] = [&amp;](Para p) { d2.command21(p);}; 
    m[22] = [&amp;](Para p) { d2.command22(p);};
</code></pre>
<p>Und natürlich müssen d1 und d2 noch existieren, wenn die Funktionen aufgerufen werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2505602</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2505602</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Mon, 15 Aug 2016 19:18:19 GMT</pubDate></item><item><title><![CDATA[Reply to Map mit std::function on Mon, 15 Aug 2016 19:23:08 GMT]]></title><description><![CDATA[<p>Ok,</p>
<p>auf die Lösung war ich auch schon gekommen. Und klar die Instanzen von Derived1 und Derived2 müssen existieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2505603</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2505603</guid><dc:creator><![CDATA[ErnstH]]></dc:creator><pubDate>Mon, 15 Aug 2016 19:23:08 GMT</pubDate></item><item><title><![CDATA[Reply to Map mit std::function on Tue, 16 Aug 2016 06:33:07 GMT]]></title><description><![CDATA[<p>Wenn du eh std::function verwendest, kannst du auch std::bind verwenden um auch member funktionen in ein std::function zu packen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2505625</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2505625</guid><dc:creator><![CDATA[firefly]]></dc:creator><pubDate>Tue, 16 Aug 2016 06:33:07 GMT</pubDate></item></channel></rss>