Serial Port Functor
-
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; }
-
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; }
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. -
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; }
what does that offer over a normal function call? besides increased unreadability...
-
what does that offer over a normal function call? besides increased unreadability...
Nothing