About some syntax
-
//---------------------------------------------------------------------------- // 4.2 How to Implement Functors // 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. instantiate objects of TClassA and TClassB TClassA objA; TClassB objB; // 2. instantiate TSpecificFunctor objects ... // a ) functor which encapsulates pointer to object and to member of TClassA TSpecificFunctor specFuncA(&objA, TClassA::Display); // b) functor which encapsulates pointer to object and to member of TClassB TSpecificFunctor specFuncB(&objB, &TClassB::Display); // 3. create array with pointers to TFunctor, the base class and ... TFunctor** vTable = new
-
//---------------------------------------------------------------------------- // 4.2 How to Implement Functors // 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. instantiate objects of TClassA and TClassB TClassA objA; TClassB objB; // 2. instantiate TSpecificFunctor objects ... // a ) functor which encapsulates pointer to object and to member of TClassA TSpecificFunctor specFuncA(&objA, TClassA::Display); // b) functor which encapsulates pointer to object and to member of TClassB TSpecificFunctor specFuncB(&objB, &TClassB::Display); // 3. create array with pointers to TFunctor, the base class and ... TFunctor** vTable = new
It's C++, not C#. You can read about functors here: http://www.codeproject.com/vcpp/stl/functor.asp[^] Alexandre Kojevnikov MCAD charter member Leuven, Belgium
-
It's C++, not C#. You can read about functors here: http://www.codeproject.com/vcpp/stl/functor.asp[^] Alexandre Kojevnikov MCAD charter member Leuven, Belgium
The problems are: 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]; Can you tell me their meaning? Thank you again 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. instantiate objects of TClassA and TClassB TClassA objA; TClassB objB; // 2. instantiate TSpecificFunctor objects ... // a ) functor which encapsulates pointer to object and to member of TClassA TSpecificFunctor specFuncA(&objA, TClassA::Display); // b) functor which encapsulates pointer to object and to member of TClassB TSpecificFunctor specFunc
-
The problems are: 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]; Can you tell me their meaning? Thank you again 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. instantiate objects of TClassA and TClassB TClassA objA; TClassB objB; // 2. instantiate TSpecificFunctor objects ... // a ) functor which encapsulates pointer to object and to member of TClassA TSpecificFunctor specFuncA(&objA, TClassA::Display); // b) functor which encapsulates pointer to object and to member of TClassB TSpecificFunctor specFunc
Aleon666 wrote: virtual void operator()(const char* string)=0; // call using operator This is an abstract virtual function. The class containing an abstract virtual function cannot be instantiated, and is intended to be a base class. Aleon666 wrote: template class TSpecificFunctor : public TFunctor I guess this should look like this:
template<class TClass> class TSpecificFunctor : public TFunctor
This is a declaration of a template class. I think you better read on templates in MSDN. Aleon666 wrote: TSpecificFunctor specFuncA(&objA, TClassA::Display); I again guess it should be:TSpecificFunctor<TClassA> specFuncA(&objA, TClassA::Display);
This line creates an instance of TSpecificFunctor object, passing TClassA as a template argument, and (&objA, TClassA::Display) as constructor arguments. Aleon666 wrote: TFunctor** vTable = new TFunctor*[2]; This creates an array which contains two pointers toTFunctor
objects. This array is then initialized with two created functors:specFuncA
andspecFuncB
. HTH Alexandre Kojevnikov MCAD charter member Leuven, Belgium -
Aleon666 wrote: virtual void operator()(const char* string)=0; // call using operator This is an abstract virtual function. The class containing an abstract virtual function cannot be instantiated, and is intended to be a base class. Aleon666 wrote: template class TSpecificFunctor : public TFunctor I guess this should look like this:
template<class TClass> class TSpecificFunctor : public TFunctor
This is a declaration of a template class. I think you better read on templates in MSDN. Aleon666 wrote: TSpecificFunctor specFuncA(&objA, TClassA::Display); I again guess it should be:TSpecificFunctor<TClassA> specFuncA(&objA, TClassA::Display);
This line creates an instance of TSpecificFunctor object, passing TClassA as a template argument, and (&objA, TClassA::Display) as constructor arguments. Aleon666 wrote: TFunctor** vTable = new TFunctor*[2]; This creates an array which contains two pointers toTFunctor
objects. This array is then initialized with two created functors:specFuncA
andspecFuncB
. HTH Alexandre Kojevnikov MCAD charter member Leuven, BelgiumThank you again!