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 / C++ / MFC
  4. Here are some syntax

Here are some syntax

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++delphiquestion
4 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

    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

    R 1 Reply Last reply
    0
    • F FlyingDancer

      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

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      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 to TFunctor objects. Aleon666 wrote: delete[] vTable; This deletes the array of pointers to TFunctor objects created above. Aleon666 wrote: (*vTable[1])("TClassB::Display called!"); "*vTable[1]" is put in brackets so that the "*" would apply to the vTable[1] part. It needs to be there because vTable[1] is a pointer to a TFunctor, 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 call

      vTable[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"

      F 1 Reply Last reply
      0
      • R Ryan Binns

        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 to TFunctor objects. Aleon666 wrote: delete[] vTable; This deletes the array of pointers to TFunctor objects created above. Aleon666 wrote: (*vTable[1])("TClassB::Display called!"); "*vTable[1]" is put in brackets so that the "*" would apply to the vTable[1] part. It needs to be there because vTable[1] is a pointer to a TFunctor, 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 call

        vTable[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"

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

        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:

        R 1 Reply Last reply
        0
        • F FlyingDancer

          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:

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #4

          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"

          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