funktions pointer
-
Das ist ebenfalls moeglich. Ansonsten wird dem Funktionszeiger implizit die
Adresse der Funktion zugewiesen.mfg
v R
-
virtuell Realisticer schrieb:
Das ist ebenfalls moeglich. Ansonsten wird dem Funktionszeiger implizit die
Adresse der Funktion zugewiesen.mfg
v Rwas heisst imlizit...macht das der compiler?
cu
-
Jepp
mfg
v R
-
Implizit heisst "ohne es auszudrücken".
-
so, ich hab nun mal versucht dem funktionspointer adressen von methoden (nennt man glaube ich besser methodenpointer...) zu zuweisen die nicht static sind...hab ne instanz der klasse erstellt...und beim übergeben hat man ja noch den this pointer versteckt im hintergrund...?
class CalcMethodCollection { private: CalcMethodCollection ( const CalcMethodCollection&) {} const CalcMethodCollection& operator = ( const CalcMethodCollection&) { return *this; } public: CalcMethodCollection(); unsigned int aCalculatorMethod(unsigned int val) { return (val * 5); } unsigned int anotherCalculatorMethod(unsigned int val) { return (val + 7); } }; int main() { unsigned int (CalcMethodCollection::*calcFunction)(unsigned int); CalcMethodCollection method_collection1(); calcFunction = &CalcMethodCollection::aCalculatorMethod; cout << "calling calcFunction(3): " << (method_collection1.*calcFunction)(3) << endl; calcFunction = &CalcMethodCollection::anotherCalculatorMethod; cout << "calling calcFunction(3): " << (method_collection1.*calcFunction)(3) << endl; cin.get(); return 0; }
nun regt sich der compiler auf:
main.cpp(196) : error C2296: '.*' : illegal, left operand has type 'CalcMethodCollection (__cdecl *)(void)' main.cpp(199) : error C2296: '.*' : illegal, left operand has type 'CalcMethodCollection (__cdecl *)(void)'
ich bin mir auch nicht ganz sicher ob man die methode mit dem operator:
.* aufruft...wenn ja warum?cu
-
Lass die Klammern bei method_collection1 beim deklarieren weg. Frag mich bitte nicht warum genau.
-
YASC schrieb:
Lass die Klammern bei method_collection1 beim deklarieren weg. Frag mich bitte nicht warum genau.
IMHO deklariert er damit eine Funktion, welche keine Parameter erwartet und eine
Kopie eines 'CalcMethodCollection'-Objektes zurueckgibt und das will er ja nicht.mfg
v RPS: Du musst deinen CTor noch deklarieren.
-
ja hab ich gesehen;-)
cout << "calling calcFunction(3): " << (&method_collection1->*calcFunction)(3) << endl;
warum macht da & da einen pointer daraus? und man darf -> aufrufen und der compiler regt dich nicht auf?
cu
-
Mit method_collection1 hast du ein 'CalcMethodCollection' und mit
&method_collection1 bekommst du ein 'CalcMethodCollection*'. Du bekommst also
die Adresse von 'method_collection1'. Um auf den Inhalt zugreifen zu koennen,
musst du den Zeiger dereferenzieren. Also schreibst du entweder
(*method_collection1). oder du nutzt den ->-Operator.mfg
v R
-
bei mir funktioniert es so:
unsigned int (CalcMethodCollection::*calcFunction)(unsigned int); CalcMethodCollection method_collection1; calcFunction = &CalcMethodCollection::aCalculatorMethod; cout << "calling calcFunction(3): " << (method_collection1.*calcFunction)(3) << endl; calcFunction = &CalcMethodCollection::anotherCalculatorMethod; cout << "calling calcFunction(3): " << (method_collection1.*calcFunction)(3) << endl; cin.get();
die 2 runden klammern bei
CalcMethodCollection method_collection1;
brauchst du nur wenn du keinen standard Konstruktor verwendest:
class CalcMethodCollection { private: CalcMethodCollection ( const CalcMethodCollection&) {} const CalcMethodCollection& operator = ( const CalcMethodCollection&) { return *this; } public: CalcMethodCollection(int t) { } unsigned int aCalculatorMethod(unsigned int val) { return (val * 5); } unsigned int anotherCalculatorMethod(unsigned int val) { return (val + 7); } }; int main() { unsigned int (CalcMethodCollection::*calcFunction)(unsigned int); CalcMethodCollection method_collection1(5); calcFunction = &CalcMethodCollection::aCalculatorMethod; cout << "calling calcFunction(3): " << (method_collection1.*calcFunction)(3) << endl; calcFunction = &CalcMethodCollection::anotherCalculatorMethod; cout << "calling calcFunction(3): " << (method_collection1.*calcFunction)(3) << endl; cin.get(); return 0; }
gruß
firefly
-
vielen dank!
arbeitet virtual im hintergrund auch mit funktions bzw memberpointer?
is gibt ja so ne art vtable...das so ein array wo alle adressen der methoden drinnen sind?cu
-
cmarc schrieb:
arbeitet virtual im hintergrund auch mit funktions bzw memberpointer?
is gibt ja so ne art vtable...das so ein array wo alle adressen der methoden drinnen sind?Im Prinzip meistens ja, aber der Standard legt das, wie so oft, nicht fest.
Moritz
-
kann mir einer sagen, wie ich funktionspointer verwende in verschiedenen klassen?
z.b.
KlasseA mit Methode Fkt1() die wiederrum auf methodenvariablen zugreift.
Jetzt will ich in KlasseB die Fkt1() von KlasseA zugreifen.Kann mir da einer ein konkretes Fallbeispiel hinschreiben? Es stimmt doch die tatsache, das man eine nichtstatische Methode NIE in einer anderen Klasse aufrufen kann. Kann mir einer eine "...for dummies" antwort geben ( wäre auch ein fall für die FAQ dann )