<?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[Functor-Anfängerfrage]]></title><description><![CDATA[<p>Hallo,</p>
<p>kann mir jemand sagen, warum der Aufruf eines Functors so klappt:</p>
<pre><code class="language-cpp">#define output(c) std::cout&lt;&lt;c;
#define outputLine(c) std::cout&lt;&lt;c&lt;&lt;&quot;\r\n&quot;;

class TFunctor
{
public:
  virtual void operator()()=0;
  virtual void Call()=0;
};

template &lt;class TClass&gt; class TGameFunctor : public TFunctor
{
private:
  void (TClass::*fpt)();
  TClass* pt2Object;

public:
  TGameFunctor(TClass* _pt2Object, void(TClass::*_fpt)()) { pt2Object = _pt2Object;  fpt=_fpt; };
  virtual void operator()() { (*pt2Object.*fpt)();};
  virtual void Call() { (*pt2Object.*fpt)();};
};

struct MenuPoint
{
	float x1, y1, x2, y2;
	char * name;
	char * text;
	TFunctor * MethodPointer;

	MenuPoint(float _x1, float _y1, float _x2, float _y2, char * _name, char * _text, TFunctor * _MethodPointer)
	{
		x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2;
		name = _name;
		text = _text;
		MethodPointer = _MethodPointer;
	}

	MenuPoint()
	{
	}
};

class Menue
{
public:
	void AddMenuPoint(MenuPoint * newMenuPoint)
	{
		output(&quot;Aufruf ueber AddMenuPoint(): &quot;);
		pMenuPoint = newMenuPoint;
		(*pMenuPoint-&gt;MethodPointer)();
	}

	void TestMenueIntersection()
	{
		output(&quot;Aufruf ueber TestMenueIntersection(): &quot;);
		(*pMenuPoint-&gt;MethodPointer)();
	}

	MenuPoint * pMenuPoint;
};

class Game
{
public:
	void NewGame()
	{
		std::cout&lt;&lt;&quot;Neues Spiel wird gestartet!\r\n&quot;;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	Menue * pMenue = new Menue();
	Game * pGame = new Game();

	pMenue-&gt;AddMenuPoint(&amp;MenuPoint(0, 0, 100, 20, &quot;Testname&quot;, &quot;Testtext&quot;, &amp;TGameFunctor&lt;Game&gt;(pGame, &amp;Game::NewGame)));
	pMenue-&gt;TestMenueIntersection();

	system(&quot;PAUSE&quot;);
}
</code></pre>
<p>nicht aber, wenn ich das Ganze über eine weitere Klasse laufen lasse, die pMenue und pGame initialisiert?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/220160/functor-anfängerfrage</link><generator>RSS for Node</generator><lastBuildDate>Wed, 07 Oct 2026 13:07:31 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/220160.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 12 Aug 2008 15:12:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Functor-Anfängerfrage on Tue, 12 Aug 2008 15:12:01 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>kann mir jemand sagen, warum der Aufruf eines Functors so klappt:</p>
<pre><code class="language-cpp">#define output(c) std::cout&lt;&lt;c;
#define outputLine(c) std::cout&lt;&lt;c&lt;&lt;&quot;\r\n&quot;;

class TFunctor
{
public:
  virtual void operator()()=0;
  virtual void Call()=0;
};

template &lt;class TClass&gt; class TGameFunctor : public TFunctor
{
private:
  void (TClass::*fpt)();
  TClass* pt2Object;

public:
  TGameFunctor(TClass* _pt2Object, void(TClass::*_fpt)()) { pt2Object = _pt2Object;  fpt=_fpt; };
  virtual void operator()() { (*pt2Object.*fpt)();};
  virtual void Call() { (*pt2Object.*fpt)();};
};

struct MenuPoint
{
	float x1, y1, x2, y2;
	char * name;
	char * text;
	TFunctor * MethodPointer;

	MenuPoint(float _x1, float _y1, float _x2, float _y2, char * _name, char * _text, TFunctor * _MethodPointer)
	{
		x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2;
		name = _name;
		text = _text;
		MethodPointer = _MethodPointer;
	}

	MenuPoint()
	{
	}
};

class Menue
{
public:
	void AddMenuPoint(MenuPoint * newMenuPoint)
	{
		output(&quot;Aufruf ueber AddMenuPoint(): &quot;);
		pMenuPoint = newMenuPoint;
		(*pMenuPoint-&gt;MethodPointer)();
	}

	void TestMenueIntersection()
	{
		output(&quot;Aufruf ueber TestMenueIntersection(): &quot;);
		(*pMenuPoint-&gt;MethodPointer)();
	}

	MenuPoint * pMenuPoint;
};

class Game
{
public:
	void NewGame()
	{
		std::cout&lt;&lt;&quot;Neues Spiel wird gestartet!\r\n&quot;;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	Menue * pMenue = new Menue();
	Game * pGame = new Game();

	pMenue-&gt;AddMenuPoint(&amp;MenuPoint(0, 0, 100, 20, &quot;Testname&quot;, &quot;Testtext&quot;, &amp;TGameFunctor&lt;Game&gt;(pGame, &amp;Game::NewGame)));
	pMenue-&gt;TestMenueIntersection();

	system(&quot;PAUSE&quot;);
}
</code></pre>
<p>nicht aber, wenn ich das Ganze über eine weitere Klasse laufen lasse, die pMenue und pGame initialisiert?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1563451</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1563451</guid><dc:creator><![CDATA[trsdrg]]></dc:creator><pubDate>Tue, 12 Aug 2008 15:12:01 GMT</pubDate></item><item><title><![CDATA[Reply to Functor-Anfängerfrage on Tue, 12 Aug 2008 15:24:29 GMT]]></title><description><![CDATA[<p>Du übergibst die Adresse eines temporären Objekts. Diese Adresse wird vermutlich gespeichert, und erst nachdem der Ausdruck abgearbeitet ist, aufgerufen. Dann existiert der Functor aber schon lange nicht mehr.</p>
<p>Abhilfe würde ein Smartpointer des TFunctor schaffen, der als Kopie abgelegt wird. Das Functor-Objekt muss dann aber mit new angelegt werden. Alternativ kannst Du Dir eine eigene Functor-Verwaltungsklasse schreiben, die dann auch als Kopie herumgereicht wird. Das hätte zudem den Vorteil, dass Du eventuelle weitere Nachfahren von TFunctor durch Hinzufügen eines weiteren Konstruktors auch mit dieser Klasse erschlagen könntest.</p>
<pre><code class="language-cpp">/* Annahme: TFunctor heißt TFunctorImpl und TGameFunctor heißt TGameFunctorImpl */
class TFunctor
{
    template&lt;class TClass&gt;
    TFunctor(TClass* _pt2Object, void(TClass::*_fpt)())
        : m_functor(new TGameFunctorImpl(_ptObject, _fpt)) {}
    ~TFunctor() { delete m_functor; }

    void operator()() { m_functor-&gt;Call(); }
    void Call() { m_functor-&gt;Call(); }
private:
    TFunctorImpl* m_functor;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1563453</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1563453</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Tue, 12 Aug 2008 15:24:29 GMT</pubDate></item></channel></rss>