<?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[Funktionenzeiger]]></title><description><![CDATA[<p>Hallo!<br />
warum funzt folgenes nicht?</p>
<pre><code class="language-cpp">template&lt;class T&gt;
class CommandBase
{
	//Deklaration Function Pointer
public:
	typedef void (T::*Base_FunctionPointer)(void);

	Base_FunctionPointer FktPtr;
};

class CommandChild1 //: public CommandBase
{

public:

	void Kommando1(){cout&lt;&lt;&quot;CommandChild1 - Kommando1&quot;&lt;&lt;endl;};

};

int main (void)
{

	 CommandBase&lt;CommandChild1&gt; *pBase = new CommandChild1;
</code></pre>
<p>Meldung:<br />
'Initialisierung': 'CommandChild1 *' kann nicht in 'CommandBase&lt;T&gt; *' konvertiert werden</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/135427/funktionenzeiger</link><generator>RSS for Node</generator><lastBuildDate>Fri, 28 Aug 2026 23:19:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/135427.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 02 Feb 2006 08:43:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Funktionenzeiger on Thu, 02 Feb 2006 08:43:09 GMT]]></title><description><![CDATA[<p>Hallo!<br />
warum funzt folgenes nicht?</p>
<pre><code class="language-cpp">template&lt;class T&gt;
class CommandBase
{
	//Deklaration Function Pointer
public:
	typedef void (T::*Base_FunctionPointer)(void);

	Base_FunctionPointer FktPtr;
};

class CommandChild1 //: public CommandBase
{

public:

	void Kommando1(){cout&lt;&lt;&quot;CommandChild1 - Kommando1&quot;&lt;&lt;endl;};

};

int main (void)
{

	 CommandBase&lt;CommandChild1&gt; *pBase = new CommandChild1;
</code></pre>
<p>Meldung:<br />
'Initialisierung': 'CommandChild1 *' kann nicht in 'CommandBase&lt;T&gt; *' konvertiert werden</p>
]]></description><link>https://www.c-plusplus.net/forum/post/983675</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/983675</guid><dc:creator><![CDATA[Theresa]]></dc:creator><pubDate>Thu, 02 Feb 2006 08:43:09 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionenzeiger on Thu, 02 Feb 2006 08:48:35 GMT]]></title><description><![CDATA[<p>Du mußt Child von (einer passenden Spezialisierung von) Bas ableiten, damit das klappen kann:</p>
<pre><code class="language-cpp">class CommandChild : public CommandBase&lt;CommandChild&gt;
{
  ...
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/983682</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/983682</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 02 Feb 2006 08:48:35 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionenzeiger on Thu, 02 Feb 2006 08:51:20 GMT]]></title><description><![CDATA[<p>Danke CStoll.<br />
Versuche gerade die Hinweise von Gestern im Detail zu verstehen!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/983686</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/983686</guid><dc:creator><![CDATA[Theresa]]></dc:creator><pubDate>Thu, 02 Feb 2006 08:51:20 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionenzeiger on Thu, 02 Feb 2006 09:09:22 GMT]]></title><description><![CDATA[<p>Ist es nun möglich,<br />
aus main() über FktPtr CommandChild1::Kommando1() aufzurufen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/983699</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/983699</guid><dc:creator><![CDATA[Theresa]]></dc:creator><pubDate>Thu, 02 Feb 2006 09:09:22 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionenzeiger on Thu, 02 Feb 2006 09:38:54 GMT]]></title><description><![CDATA[<p>Du kannst FktPtr einen Pointer auf diese Funktion nicht ablegen, weil Kommando1 nicht statisch ist.</p>
<p>Was willst du überhaupt damit machen? Man kommt manchmal nicht drumrum Funktionspointer zu verwenden, ansonsten würde ich jedoch dazu anraten Schnittstellen-Klassen mit virtuellen Funktionen stattdessen zu verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/983718</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/983718</guid><dc:creator><![CDATA[Mathias]]></dc:creator><pubDate>Thu, 02 Feb 2006 09:38:54 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionenzeiger on Thu, 02 Feb 2006 09:51:25 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/10111">@Mathias</a></p>
<p>gib mal n Beispiel für Schnittst. klassen mit virt. Fkt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/983734</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/983734</guid><dc:creator><![CDATA[Theresa]]></dc:creator><pubDate>Thu, 02 Feb 2006 09:51:25 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionenzeiger on Thu, 02 Feb 2006 09:55:44 GMT]]></title><description><![CDATA[<p>Nimm doch <a href="http://www.c-plusplus.net/forum/viewtopic-var-p-is-982890.html#982890" rel="nofollow">das hier</a> als Beispiel (Command wäre die Schnittstellenklasse, CmdFileOpen etc. bieten dann eine konkrete Implementierung an).</p>
<p>(PS: Das stammt aus deinem Thread von gestern ;))</p>
]]></description><link>https://www.c-plusplus.net/forum/post/983740</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/983740</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 02 Feb 2006 09:55:44 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionenzeiger on Thu, 02 Feb 2006 10:09:48 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">class Interface
{
public:
   virtual int DoIt () { return 0; }
};

class Child : public Interface
{
public:
   virtual int DoIt () { return 1; }
};

int main ()
{
    Interface * pSomething = new Child        ();
    int         i          = pSomething-&gt;DoIt (); 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/983744</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/983744</guid><dc:creator><![CDATA[Mathias]]></dc:creator><pubDate>Thu, 02 Feb 2006 10:09:48 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionenzeiger on Thu, 02 Feb 2006 11:09:02 GMT]]></title><description><![CDATA[<p>Es gibt ne bessere Möglichkeit das ganze ohne virtuelle Funktionen im ConcreteCommand zu machen. Und ohne dass ein ConcreteCommand von einem AbstractCommand erbt. Das ganze funktioniert mit Templates.</p>
<p>Möchstest Du die Commands irgendwo verwalten, dann must du zunächst aber eine Stammklasse ohne Template erstellen.</p>
<p>Das ganze ist ungetestet.</p>
<pre><code class="language-cpp">struct BaseCommand
{
   virtual void operator() = 0;
};
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
<p>Jetzt die Klasse die eine Funktion kapselt.</p>
<pre><code class="language-cpp">template &lt;class T&gt;
struct AbstractCommand : public BaseCommand
{
   typename void (T::*MyFunc)();

   AbstractCommand(T* t, MyFunc func)
      : obj_(t), func_(func)
   {}
   void operator()
   { obj-&gt;*func(); }

private:
   T* obj_;
   MyFunc* func
};
</code></pre>
<p>So kannst du auch übringens auch &quot;normale&quot; Funktionen kapseln. Wie, kannst du dir selbst überlegen.</p>
<p>Verwenden tust du das so:</p>
<pre><code class="language-cpp">// MyObject weiß nichts von einem AbstractCommand
class MyObject 
{
   void foo()
   { std::cout &lt;&lt; &quot;MyObject::foo\n&quot;; }
};

int main()
{
   AbstractCommand&lt;MyObject&gt; cmd(new MyObject, &amp;MyObject::foo);
   cmd(); // &lt;-- hier kommt der aufruf.
}
</code></pre>
<p>Wie gesagt, es ist ungetestet, sollte aber funktionieren.</p>
<p>Was mir noch einfällt: Man könnte eigentlich auch die BaseCommand als ein Template Parameter bestimmen lassen und somit festlegen ob operator() virtuell ist oder nicht:</p>
<pre><code class="language-cpp">// Reduziert auf das Nötigste...

template &lt;bool HasBaseCmd&gt;
struct ConstructCmd
{
   struct AbstractCommand : public BaseCmd
   { ... };
};

template &lt;&gt;
struct ConstructCmd&lt;false&gt;
{ 
   struct AbstractCommand
   { ... };
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/983807</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/983807</guid><dc:creator><![CDATA[adxin]]></dc:creator><pubDate>Thu, 02 Feb 2006 11:09:02 GMT</pubDate></item></channel></rss>