Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. About some syntax

About some syntax

Scheduled Pinned Locked Moved C#
tutorialdata-structures
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FlyingDancer
    wrote on last edited by
    #1

    //---------------------------------------------------------------------------- // 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

    A 1 Reply Last reply
    0
    • F FlyingDancer

      //---------------------------------------------------------------------------- // 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

      A Offline
      A Offline
      Alexander Kojevnikov
      wrote on last edited by
      #2

      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

      F 1 Reply Last reply
      0
      • A Alexander Kojevnikov

        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

        F Offline
        F Offline
        FlyingDancer
        wrote on last edited by
        #3

        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

        A 1 Reply Last reply
        0
        • F FlyingDancer

          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

          A Offline
          A Offline
          Alexander Kojevnikov
          wrote on last edited by
          #4

          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 to TFunctor objects. This array is then initialized with two created functors: specFuncA and specFuncB. HTH Alexandre Kojevnikov MCAD charter member Leuven, Belgium

          F 1 Reply Last reply
          0
          • A Alexander Kojevnikov

            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 to TFunctor objects. This array is then initialized with two created functors: specFuncA and specFuncB. HTH Alexandre Kojevnikov MCAD charter member Leuven, Belgium

            F Offline
            F Offline
            FlyingDancer
            wrote on last edited by
            #5

            Thank you again!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups