<?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, Functionszeiger]]></title><description><![CDATA[<p>Guten Tag,</p>
<p>habe mir schon sehr viele Programmtexte angesehen mit Funktionsobjekten (Funktoren).</p>
<p>Jetzt war meine Idee, daß ich eine Klasse für einen functor anlege und dieses Object in einer anderen Klasse verwende...</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

class Functor
{
protected:
  // function to call a memberfunction to display the data contend of a variable
  std::string (*functionptr)();
public:
  Functor();
  std::string operator()(std::string (*functionptr)());
};

Functor::Functor():functionptr(0) 
{}

std::string Functor::operator()(std::string (*fptr)())
{
  functionptr=fptr;
}

class address
{
protected:
  std::string firstName;
  std::string secondName;

  std::string (address::*pfn)();     // this is a functionpointer with the name pfn

public:
  Functor f_pfn;
  address(std::string _firstName=&quot;mein&quot;, std::string _secondName=&quot;Name&quot;);
  std::string getfirstName();
  std::string getsecondName();
  void ausgabeinit();
  void execute();
};

address::address(std::string vname, std::string nname):firstName(vname), secondName(nname), pfn(0)
{
	std::cout &lt;&lt; &quot;\nconstructor pfn: &quot; &lt;&lt; pfn &lt;&lt; std::endl;
}

std::string address::getfirstName()
{
  return firstName;
}

std::string address::getsecondName()
{
  return secondName;
}

void address::ausgabeinit()
{
	pfn=&amp;address::getfirstName;
	std::cout &lt;&lt; &quot;\nausgabeinit() pfn: &quot; &lt;&lt; pfn &lt;&lt; std::endl;
	std::cout &lt;&lt; (this-&gt;*pfn)() &lt;&lt; std::endl;
}

void address::execute()
{
	std::cout &lt;&lt; &quot;\naddress::execute(): &quot; &lt;&lt; (this-&gt;*pfn)();
}

int main()
{
  Functor malso;
  address icke;
  std::cout &lt;&lt; &quot;Vorname: &quot; &lt;&lt; icke.getfirstName() &lt;&lt; std::endl;
  icke.ausgabeinit();
  icke.execute();
  std::cout &lt;&lt; &quot;\nicke(getsecondName()): &quot; &lt;&lt; icke.(address::getsecondName()) &lt;&lt; std::endl;
  return 0;
}
</code></pre>
<p>als fehlermeldung kommt:</p>
<pre><code>g++ -o mitfunktor functorclass.cc 
functorclass.cc: In Funktion »int main()«:
functorclass.cc:72:52: Fehler: expected unqualified-id before »(« token
functorclass.cc:72:76: Fehler: Elementfunktion »std::string address::getsecondName()« kann nicht ohne Objekt aufgerufen werden
</code></pre>
<p>was muß ich tun damit ich den operator() des functors aufrufen kann? Oder besser wo mache ich den Fehler?</p>
<p>der functor ist ein public-element und müsste aufrufbar sein, ein object habe ich doch auch gebastelt... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/303982/functor-functionszeiger</link><generator>RSS for Node</generator><lastBuildDate>Sun, 09 Aug 2026 21:02:26 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/303982.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 27 May 2012 06:13:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Functor, Functionszeiger on Sun, 27 May 2012 06:13:56 GMT]]></title><description><![CDATA[<p>Guten Tag,</p>
<p>habe mir schon sehr viele Programmtexte angesehen mit Funktionsobjekten (Funktoren).</p>
<p>Jetzt war meine Idee, daß ich eine Klasse für einen functor anlege und dieses Object in einer anderen Klasse verwende...</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

class Functor
{
protected:
  // function to call a memberfunction to display the data contend of a variable
  std::string (*functionptr)();
public:
  Functor();
  std::string operator()(std::string (*functionptr)());
};

Functor::Functor():functionptr(0) 
{}

std::string Functor::operator()(std::string (*fptr)())
{
  functionptr=fptr;
}

class address
{
protected:
  std::string firstName;
  std::string secondName;

  std::string (address::*pfn)();     // this is a functionpointer with the name pfn

public:
  Functor f_pfn;
  address(std::string _firstName=&quot;mein&quot;, std::string _secondName=&quot;Name&quot;);
  std::string getfirstName();
  std::string getsecondName();
  void ausgabeinit();
  void execute();
};

address::address(std::string vname, std::string nname):firstName(vname), secondName(nname), pfn(0)
{
	std::cout &lt;&lt; &quot;\nconstructor pfn: &quot; &lt;&lt; pfn &lt;&lt; std::endl;
}

std::string address::getfirstName()
{
  return firstName;
}

std::string address::getsecondName()
{
  return secondName;
}

void address::ausgabeinit()
{
	pfn=&amp;address::getfirstName;
	std::cout &lt;&lt; &quot;\nausgabeinit() pfn: &quot; &lt;&lt; pfn &lt;&lt; std::endl;
	std::cout &lt;&lt; (this-&gt;*pfn)() &lt;&lt; std::endl;
}

void address::execute()
{
	std::cout &lt;&lt; &quot;\naddress::execute(): &quot; &lt;&lt; (this-&gt;*pfn)();
}

int main()
{
  Functor malso;
  address icke;
  std::cout &lt;&lt; &quot;Vorname: &quot; &lt;&lt; icke.getfirstName() &lt;&lt; std::endl;
  icke.ausgabeinit();
  icke.execute();
  std::cout &lt;&lt; &quot;\nicke(getsecondName()): &quot; &lt;&lt; icke.(address::getsecondName()) &lt;&lt; std::endl;
  return 0;
}
</code></pre>
<p>als fehlermeldung kommt:</p>
<pre><code>g++ -o mitfunktor functorclass.cc 
functorclass.cc: In Funktion »int main()«:
functorclass.cc:72:52: Fehler: expected unqualified-id before »(« token
functorclass.cc:72:76: Fehler: Elementfunktion »std::string address::getsecondName()« kann nicht ohne Objekt aufgerufen werden
</code></pre>
<p>was muß ich tun damit ich den operator() des functors aufrufen kann? Oder besser wo mache ich den Fehler?</p>
<p>der functor ist ein public-element und müsste aufrufbar sein, ein object habe ich doch auch gebastelt... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2215925</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2215925</guid><dc:creator><![CDATA[thepower]]></dc:creator><pubDate>Sun, 27 May 2012 06:13:56 GMT</pubDate></item><item><title><![CDATA[Reply to Functor, Functionszeiger on Sun, 27 May 2012 07:04:29 GMT]]></title><description><![CDATA[<p>Kennst du std::function?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2215933</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2215933</guid><dc:creator><![CDATA[314159265358979__]]></dc:creator><pubDate>Sun, 27 May 2012 07:04:29 GMT</pubDate></item><item><title><![CDATA[Reply to Functor, Functionszeiger on Sun, 27 May 2012 07:17:04 GMT]]></title><description><![CDATA[<p>Nach icke muss der Punkt weg. Und nach address::getSecondName die Klammern. Außerdem geht das nicht, da address::getsecondName eine Memberfunktion ist, die Funktion operator() nimmt aber einen Funktionpointer, keinen Memberfunktionpointer.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2215935</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2215935</guid><dc:creator><![CDATA[pyhax]]></dc:creator><pubDate>Sun, 27 May 2012 07:17:04 GMT</pubDate></item><item><title><![CDATA[Reply to Functor, Functionszeiger on Sun, 27 May 2012 08:05:02 GMT]]></title><description><![CDATA[<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /> Versteh ich richtig, obwohl Functor ein Attribut von address ist, darf ich nicht auf die memberfunktionen von address zugreifen?</p>
<p>nein, std::function habe ich zwar schonmal im Netz gelesen, aber kennen tu ich's nicht.</p>
<p>Bin absoluter Anfänger und versuche die &quot;logischen&quot; Grundlagen zu verstehen, damit ich weiß was ich mache wenn ich ein Programm schreibe...</p>
<p>erst mal vielen dank, dann versuch ich's mal mit deinem Tip</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2215937</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2215937</guid><dc:creator><![CDATA[thepower]]></dc:creator><pubDate>Sun, 27 May 2012 08:05:02 GMT</pubDate></item><item><title><![CDATA[Reply to Functor, Functionszeiger on Sun, 27 May 2012 08:23:15 GMT]]></title><description><![CDATA[<p>Außerdem, wo benutzt du den Funktor überhaupt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2215943</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2215943</guid><dc:creator><![CDATA[pyhax]]></dc:creator><pubDate>Sun, 27 May 2012 08:23:15 GMT</pubDate></item><item><title><![CDATA[Reply to Functor, Functionszeiger on Sun, 27 May 2012 08:31:18 GMT]]></title><description><![CDATA[<p>In dem Code versuche ich im &quot;main&quot; den Functor zu benutzen... dachte ich da ja die class address keinen operator() hat... wäre es ein Lösung in address einen operator() einzubauen der die verschiedenen Ausbage- get-funktionen aufruft <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2215944</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2215944</guid><dc:creator><![CDATA[thepower]]></dc:creator><pubDate>Sun, 27 May 2012 08:31:18 GMT</pubDate></item><item><title><![CDATA[Reply to Functor, Functionszeiger on Fri, 01 Jun 2012 09:05:42 GMT]]></title><description><![CDATA[<p>Erst mal den Code</p>
<pre><code class="language-cpp">bool Person::SetFunctionsPtr()
{
  bool (Person::*SetFunctions[PERSON_ATTRIBUTES])(std::string) = {&amp;Person::SetPersonID,  &amp;Person::SetAddress, &amp;Person::SetFirstName, &amp;Person::SetSecondName, &amp;Person::SetNameAffix, &amp;Person::SetTitle};

  std::string (Person::*GetFunctions[PERSON_ATTRIBUTES])() const={&amp;Person::GetPersonID, &amp;Person::GetSecondName, &amp;Person::GetFirstName, &amp;Person::GetNameAffix, &amp;Person::GetAddress, &amp;Person::GetTitle};

  if(SetFunctions &amp;&amp; GetFunctions)
    {
      std::cout &lt;&lt; &quot;SetFunctions: &quot; &lt;&lt; SetFunctions &lt;&lt; std::endl;
      std::cout &lt;&lt; &quot;GetFunctions: &quot; &lt;&lt; GetFunctions &lt;&lt; std::endl;

    return true;
    }
  else 
    return false;
}

bool Person::SetPerson()
{
  std::cout &lt;&lt; &quot;In SetPerson SetFunctions: &quot; &lt;&lt; Person::SetFunctions &lt;&lt; std::endl;
      std::cout &lt;&lt; &quot;In SetPerson GetFunctions: &quot; &lt;&lt; GetFunctions &lt;&lt; std::endl;
}

#include &quot;person.h&quot;

int main()
{
	Person probemensch;

	probemensch.SetPerson();

	return 0;
}
</code></pre>
<p>Kurze Erläuterung meiner Idee: Die Zeiger sind Members der Klasse person, also, so dachte ich, von allen Methoden aufrufbar. Da ich diese member öfter benutzen will habe ich sie in der klasse implementiert. Im Konstruktor bekomme ich auch addressen von den Zeigern angezeigt, aber dann in der methode SetPerson() folgendes:</p>
<pre><code>enklasse/src$ ../verwaltung 
SetFunctions: 0xbf8ab990
GetFunctions: 0xbf8ab9c0

Weiter geht's mit irgendeiner Taste...

In SetPerson SetFunctions: 0xbf8abcbc
In SetPerson GetFunctions: 0xbf8abcbc
</code></pre>
<p>Also denke ich, daß ich irgendwo einen Fehler in der syntax/logik habe da auch mit &amp;SetFunctions oder *SetFunctions nicht die adresse angezeigt wird in SetPerson wie in dem Konstruktor...</p>
<p>Leider finde ich keine Möglichkeit, bzw. habe keine Idee wie ich den zeiger aufrufen muß damit die gewünschte &quot;Adresse&quot; bzw. die Funktion hinter der Adresse ausgeführt wird...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218054</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218054</guid><dc:creator><![CDATA[thepower]]></dc:creator><pubDate>Fri, 01 Jun 2012 09:05:42 GMT</pubDate></item><item><title><![CDATA[Reply to Functor, Functionszeiger on Sat, 02 Jun 2012 08:26:38 GMT]]></title><description><![CDATA[<p>Was soll das denn alles? Worum geht's dir? Was hat da ein Elementfunktionszeiger in der Klasse address zu suchen? Warum hast du ein <code>#include &quot;person.h&quot;</code> <em>hinter</em> den Definitionen von Sachen aus der Person-Klasse? Ich glaube, du machst alles viel komplizierter als nötig ...</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

struct address
{
  std::string firstname;
  std::string secondname;
};

typedef std::string address::*amemptr;

int main()
{
  using namespace std;
  address a = { &quot;Hans&quot;, &quot;Wurst&quot; };
  amemptr m = &amp;address::firstname;
  cout &lt;&lt; (a.*m) &lt;&lt; endl;
  m = &amp;address::secondname;
  cout &lt;&lt; (a.*m) &lt;&lt; endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2218377</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218377</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Sat, 02 Jun 2012 08:26:38 GMT</pubDate></item><item><title><![CDATA[Reply to Functor, Functionszeiger on Sat, 02 Jun 2012 08:37:22 GMT]]></title><description><![CDATA[<p>Nun, ich will einen Funktionzeiger in meiner Klasse haben um dann über eine Zahl die notwendige Funktion zur Ausgabe bzw. Veränderung des Wertes der in der Variabelen steht zu verändern...</p>
<p>Weil ich &quot;träge&quot; bin dachte ich es wäre am einfachsten wenn ich einen Zeiger auf die Funktionen deklariere und definiere, damit ich sie aufrufen kann, aber ich kriegs nicht hin <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218384</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218384</guid><dc:creator><![CDATA[thepower]]></dc:creator><pubDate>Sat, 02 Jun 2012 08:37:22 GMT</pubDate></item><item><title><![CDATA[Reply to Functor, Functionszeiger on Sat, 02 Jun 2012 09:03:05 GMT]]></title><description><![CDATA[<p>thepower schrieb:</p>
<blockquote>
<p>um dann über eine Zahl die notwendige Funktion zur Ausgabe bzw. Veränderung des Wertes der in der Variabelen steht zu verändern...</p>
</blockquote>
<p>Warum? Das erschließt sich mir nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218386</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218386</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Sat, 02 Jun 2012 09:03:05 GMT</pubDate></item><item><title><![CDATA[Reply to Functor, Functionszeiger on Sat, 02 Jun 2012 11:45:20 GMT]]></title><description><![CDATA[<p>...könnte sein, daß ich falsch liege, aber ich habe im Moment folgendes im Header stehen:</p>
<pre><code class="language-cpp">// Header for class person
// 30.05.2012

#ifndef _PERSON_H
#define _PERSON_H

#ifndef STRING_H
#include &lt;string&gt;
#endif

#ifndef _GLIBCXX_FSTREAM
#include &lt;fstream&gt;
#endif

class Person
{
protected:
  // static members
  static unsigned int NumberOfPersons;
  static const int PERSON_ATTRIBUTES;

  // static methods
  // functions to Set/Get the allowed values of IDs
  static unsigned int SetNumberOfPersons();
  static unsigned int GetNumberOfPersons();

  // Tools to make functions usable
  unsigned int CountMembersFile();

  // Datamembers of person: PersonID:second name:first name:name affix (jun. or something else):Addressing:title (Dr. or so on)
  unsigned int PersonID;
  std::string SecondName;
  std::string FirstName;
  std::string NameAffix;
  std::string Address;
  std::string Title;

  // Filename in which the data is stored
  std::string FileNamePerson;

  // Filepointers
  std::fstream InFilePersons;      // Put data in File FileNamePerson
  std::fstream OutFilePersons;     // Get data from File FileNamePerson

  // Pointer to the Set-functions Set to 0 in constructor
  bool(Person::*SetFunctions[])(std::string);

  // Pointer to the Get-functions Set to 0 in constructor
  std::string (Person::*GetFunctions[])() const;

public:
  // Constructor with default values
  Person( unsigned int _PersonID=0,
	  std::string _SecondName=&quot;Scheidt&quot;,
	  std::string _FirstName=&quot;Marianne&quot;,
	  std::string _NameAffix=&quot;jun.&quot;,
	  std::string _Address=&quot;Herr&quot;,
	  std::string _Title=&quot;&quot;,
	  std::string _FileNamePerson=&quot;customerNames.dat&quot;);

  // Destructor &quot;virtual&quot; if it is needed in derived classes
  virtual ~Person()
  {}

  // Set values of the functionpointers
  virtual bool SetFunctionsptr();
#if 0
  // Set values of a whole person
  virtual bool SetPerson();

  // Set values of a person
  virtual bool SetPersonID(std::string _personID);
  virtual bool SetSecondName(std::string _in);
  virtual bool SetFirstName(std::string _in);
  virtual bool SetNameAffix(std::string _in);
  virtual bool SetAddress(std::string _in);
  virtual bool SetTitle(std::string _in);

  // function to change the Set values of a variable
  bool ChangeValues(int selection); // selection is the number of the choosen variable

  //Get-functions to &quot;use&quot; the variable values of Person
  virtual std::string GetPersonID() const;
  virtual std::string GetSecondName() const;
  virtual std::string GetFirstName() const;
  virtual std::string GetNameAffix() const;
  virtual std::string GetAddress() const;
  virtual std::string GetTitle() const;

  // Function to Set the Menue which the users need to run the application
  virtual void Menue();

  // Function to write the Person to in form:
  // Person Nummer: PersonID
  // Anrede: Address Title: Dr. med.
  // Vorname: FirstName 
  // Nachname: SecondName
  // Anhang: NameAffix
  virtual void Output() const;

  // Function to read the file with the stored Persones
  // std::string filename is the name of the file in which the 
  // Persones are stored
  bool ReadFile(std::string filename);
#endif
};
#endif
</code></pre>
<p>immper wenn ich eine set- oder getfunction benögte rufe ich sie mit ihrem namen auf, wenn ich alle brauche rufe ich 6 mal die namen von set- oder get- auf... wenn ich das ganze in ein pointerarray lege kann ich mit for(int i = 0; i &lt; sizeof(pointerarray); ++9) die aufrufe vereinfachen... dachte ich</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218448</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218448</guid><dc:creator><![CDATA[thepower]]></dc:creator><pubDate>Sat, 02 Jun 2012 11:45:20 GMT</pubDate></item></channel></rss>