<>C++ Double colon in ::

* The first one , Class scope , A variable used to indicate a class , function Human::setName(char* name);
* The second kind , namespace scope , Used to indicate the class used , Which namespace does the function belong to std::cout << "Hello World" << std::endl;
*
The third kind , global scope , Used to distinguish parts , Global . One of the most easily overlooked , Many times I write a global function or want to call a global function , But found out IDE perhaps Editor The function was not found , The reason is that the local function has the same name as the global function you want to call , And after a long time, I couldn't find the reason , Even give up the solution . In fact, the reason is because
【 local variable / function 】 And 【 global variable / function 】 The same name ,IDE It is indistinguishable , At this time add :: You can call the global function , Access to global variables .
<> for instance :

Linux Lower serial port open , closed api
// fcntl.h Global functions under file open open (const char *__path, int __oflag, ...) //
unistd.h Global functions under file extern int close (int __fd);

Because every time api It's a waste coding time , And it doesn't make much sense , I'm going to encapsulate this function into my own personal serial port library WzSerialPort.h,WzSerialPort.cpp
// WzSerialPort.h class WzSerialPort { public: // ... bool open(); void close()
; // ... };
Note the following cpp file , without ::
An error will be reported , because WzSerialPort There are functions in the library open and close, Following global function open and close The same name , If we don't make the distinction between global and local , The global function cannot be called
// WzSerialPort.cpp bool WzSerialPort::open() { if( ::open(portname,O_RDWR|
O_NOCTTY|O_NONBLOCK) != -1 ) return true; else return false; } void WzSerialPort
::close() { ::close(fd); }
Reward QR code :

Technology