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. Serial Port Functor

Serial Port Functor

Scheduled Pinned Locked Moved C / C++ / MFC
c++
4 Posts 3 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.
  • J Offline
    J Offline
    Jonathan Davies
    wrote on last edited by
    #1

    Hi, Reading the below answers regarding serial ports made me wonder how safe these (below) calls would be considered. There's no need to pass anything to the constructor as in the reasons I read for using functors, and I only use them in this form because I wrote then when using STL and they're nice one liners really.

    hPort = CPortOpener()(hWnd, wszPort);

    and

    CEditPortSettings()(hWnd, hPort, wszPort);

    which use for instance:

    class CEditPortSettings
    {
    public:
    CEditPortSettings(void);

    long operator()(HWND hWnd, HANDLE hPort, std::wstring szPort)
    {
        ...
        return anyerror;    
    }
    
    P C 2 Replies Last reply
    0
    • J Jonathan Davies

      Hi, Reading the below answers regarding serial ports made me wonder how safe these (below) calls would be considered. There's no need to pass anything to the constructor as in the reasons I read for using functors, and I only use them in this form because I wrote then when using STL and they're nice one liners really.

      hPort = CPortOpener()(hWnd, wszPort);

      and

      CEditPortSettings()(hWnd, hPort, wszPort);

      which use for instance:

      class CEditPortSettings
      {
      public:
      CEditPortSettings(void);

      long operator()(HWND hWnd, HANDLE hPort, std::wstring szPort)
      {
          ...
          return anyerror;    
      }
      
      P Offline
      P Offline
      pasztorpisti
      wrote on last edited by
      #2

      It is exactly as safe as a normal function call with the same signature in this case. Functors are better than normal function/method calls only if you want to pass a function to a template method because using function pointers is not comfortable in an object oriented language that doesn't support delegates (delegate == function pointer that has the ability to point to the method of an object). Regarding your constructor declaration: CEditPortSettings(void); You have to say (void) in the parameter list of a function only in C. In C if you don't use void in case of an empty parameter list then the function is automatically vararg. In C++ the same isn't true so using void is unnecessary. I see (void) parameter list quite rarely in C++ code.

      1 Reply Last reply
      0
      • J Jonathan Davies

        Hi, Reading the below answers regarding serial ports made me wonder how safe these (below) calls would be considered. There's no need to pass anything to the constructor as in the reasons I read for using functors, and I only use them in this form because I wrote then when using STL and they're nice one liners really.

        hPort = CPortOpener()(hWnd, wszPort);

        and

        CEditPortSettings()(hWnd, hPort, wszPort);

        which use for instance:

        class CEditPortSettings
        {
        public:
        CEditPortSettings(void);

        long operator()(HWND hWnd, HANDLE hPort, std::wstring szPort)
        {
            ...
            return anyerror;    
        }
        
        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        what does that offer over a normal function call? besides increased unreadability...

        image processing toolkits | batch image processing

        J 1 Reply Last reply
        0
        • C Chris Losinger

          what does that offer over a normal function call? besides increased unreadability...

          image processing toolkits | batch image processing

          J Offline
          J Offline
          Jonathan Davies
          wrote on last edited by
          #4

          Nothing

          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