Here are some syntax
-
Now I meet several problems 1.virtual void operator()(const char* string)=0; // call using operator 2.template class TSpecificFunctor : public TFunctor //TFunctor is a class 3.TSpecificFunctor specFuncA(&objA, TClassA::Display); 4.TFunctor** vTable = new TFunctor*[2]; 5.delete[] vTable; 6.(*vTable[1]) ("TClassB::Display called!"); // via operator "()" //Why is Table[1]" put into the bracket? Can you tell me their meaning? Thank you in advance! The following codes you can refer: // This code was compiled and tested with Borland C++ Builder 5.0. Let me know // if there is something I should mention for the use with other compilers. #pragma hdrstop // Borland C++ Builder specific #pragma argsused // do. #include // due to: cout // abstract base class class TFunctor { public: // two possible functions to call member function. virtual cause derived // classes will use a pointer to an object and a pointer to a member function // to make the function call virtual void operator()(const char* string)=0; // call using operator virtual void Call(const char* string)=0; // call using function }; // derived template class template class TSpecificFunctor : public TFunctor { private: void (TClass::*fpt)(const char*); // pointer to member function TClass* pt2Object; // pointer to object public: // constructor - takes pointer to an object and pointer to a member and stores // them in two private variables TSpecificFunctor(TClass* _pt2Object, void(TClass::*_fpt)(const char*)) { pt2Object = _pt2Object; fpt=_fpt; }; // override operator "()" virtual void operator()(const char* string) { (*pt2Object.*fpt)(string);}; // execute member function // override function "Call" virtual void Call(const char* string) { (*pt2Object.*fpt)(string);}; // execute member function }; //----------------------------------------------------------------------------------------- // 4.3 Example of How to Use Functors // dummy class A class TClassA{ public: TClassA(){}; void Display(const char* text) { cout << text << endl; }; /* more of TClassA */ }; // dummy class B class TClassB{ public: TClassB(){}; void Display(const char* text) { cout << text << endl; }; /* more of TClassB */ }; // main program int main(int argc, char* argv[]) { // 1. insta
-
Now I meet several problems 1.virtual void operator()(const char* string)=0; // call using operator 2.template class TSpecificFunctor : public TFunctor //TFunctor is a class 3.TSpecificFunctor specFuncA(&objA, TClassA::Display); 4.TFunctor** vTable = new TFunctor*[2]; 5.delete[] vTable; 6.(*vTable[1]) ("TClassB::Display called!"); // via operator "()" //Why is Table[1]" put into the bracket? Can you tell me their meaning? Thank you in advance! The following codes you can refer: // This code was compiled and tested with Borland C++ Builder 5.0. Let me know // if there is something I should mention for the use with other compilers. #pragma hdrstop // Borland C++ Builder specific #pragma argsused // do. #include // due to: cout // abstract base class class TFunctor { public: // two possible functions to call member function. virtual cause derived // classes will use a pointer to an object and a pointer to a member function // to make the function call virtual void operator()(const char* string)=0; // call using operator virtual void Call(const char* string)=0; // call using function }; // derived template class template class TSpecificFunctor : public TFunctor { private: void (TClass::*fpt)(const char*); // pointer to member function TClass* pt2Object; // pointer to object public: // constructor - takes pointer to an object and pointer to a member and stores // them in two private variables TSpecificFunctor(TClass* _pt2Object, void(TClass::*_fpt)(const char*)) { pt2Object = _pt2Object; fpt=_fpt; }; // override operator "()" virtual void operator()(const char* string) { (*pt2Object.*fpt)(string);}; // execute member function // override function "Call" virtual void Call(const char* string) { (*pt2Object.*fpt)(string);}; // execute member function }; //----------------------------------------------------------------------------------------- // 4.3 Example of How to Use Functors // dummy class A class TClassA{ public: TClassA(){}; void Display(const char* text) { cout << text << endl; }; /* more of TClassA */ }; // dummy class B class TClassB{ public: TClassB(){}; void Display(const char* text) { cout << text << endl; }; /* more of TClassB */ }; // main program int main(int argc, char* argv[]) { // 1. insta
Aleon666 wrote: virtual void operator()(const char* string)=0; This is a pure-virtual function. It has no definition, and must be implemented in a derived class. Aleon666 wrote: template <class TClass> class TSpecificFunctor : public TFunctor This is a template class defintion. It allows a class to be customised using varying parameter - it's a template for producing more specialised classes. Aleon666 wrote: TSpecificFunctor specFuncA(&objA, TClassA::Display); This creates an instance of the
TSpecificFunctor
class that has been specialised with the parameter "TClassA
" Aleon666 wrote: TFunctor** vTable = new TFunctor*[2]; This creates an array of 2 pointers toTFunctor
objects. Aleon666 wrote: delete[] vTable; This deletes the array of pointers toTFunctor
objects created above. Aleon666 wrote: (*vTable[1])("TClassB::Display called!"); "*vTable[1]
" is put in brackets so that the "*" would apply to thevTable[1]
part. It needs to be there becausevTable[1]
is a pointer to aTFunctor
, but the object itself is needed - it must be dereferenced first. If the brackets were not there, the code would not compile because it will try to callvTable[1]("TClassB::Display called!");
which is non-valid code because
vTable[1]
is a pointer.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Aleon666 wrote: virtual void operator()(const char* string)=0; This is a pure-virtual function. It has no definition, and must be implemented in a derived class. Aleon666 wrote: template <class TClass> class TSpecificFunctor : public TFunctor This is a template class defintion. It allows a class to be customised using varying parameter - it's a template for producing more specialised classes. Aleon666 wrote: TSpecificFunctor specFuncA(&objA, TClassA::Display); This creates an instance of the
TSpecificFunctor
class that has been specialised with the parameter "TClassA
" Aleon666 wrote: TFunctor** vTable = new TFunctor*[2]; This creates an array of 2 pointers toTFunctor
objects. Aleon666 wrote: delete[] vTable; This deletes the array of pointers toTFunctor
objects created above. Aleon666 wrote: (*vTable[1])("TClassB::Display called!"); "*vTable[1]
" is put in brackets so that the "*" would apply to thevTable[1]
part. It needs to be there becausevTable[1]
is a pointer to aTFunctor
, but the object itself is needed - it must be dereferenced first. If the brackets were not there, the code would not compile because it will try to callvTable[1]("TClassB::Display called!");
which is non-valid code because
vTable[1]
is a pointer.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Thank you very much! I have been illuminated by you. Thanks again Today is valentine's day for our chinese people Will you send some presents to your lover if he/she is in China :) :rose::rose::rose:
-
Thank you very much! I have been illuminated by you. Thanks again Today is valentine's day for our chinese people Will you send some presents to your lover if he/she is in China :) :rose::rose::rose:
You're welcome :) Aleon666 wrote: Today is valentine's day for our chinese people Will you send some presents to your lover if he/she is in China Nope, she's in Australia :)
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"