Why a HANDLE created by CreateFile can be assigned to an object
-
Some code segment in chapter 10 of "Windows via C/C++"(5th Edition) is
// Open the source file without buffering & get its size
CEnsureCloseFile hFileSrc = CreateFile(pszFileSrc,
GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_FLAG_NO_BUFFERING |
FILE_FLAG_OVERLAPPED, NULL);Here "CEnsureCloseFile" is a class. The Author define Template class "CEnsureCleanup" which was used to clean some objects, then use a this Template define class CEnsureCloseFile My question is why the HANDLE created by "CreateFile" can be assigned to object hFileSrc . It seems rare. Do you offen do sth like this?
-
Some code segment in chapter 10 of "Windows via C/C++"(5th Edition) is
// Open the source file without buffering & get its size
CEnsureCloseFile hFileSrc = CreateFile(pszFileSrc,
GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_FLAG_NO_BUFFERING |
FILE_FLAG_OVERLAPPED, NULL);Here "CEnsureCloseFile" is a class. The Author define Template class "CEnsureCleanup" which was used to clean some objects, then use a this Template define class CEnsureCloseFile My question is why the HANDLE created by "CreateFile" can be assigned to object hFileSrc . It seems rare. Do you offen do sth like this?
Since it's just a pointer, it can be set to anything that can handle that appropriately (the constructor for this object likely takes in that type of HANDLE). His class probably just makes sure a file is properly closed without requiring you to explicitly close the file (open file handles can be an issue). http://en.wikipedia.org/wiki/Assignment_operator_(C%2B%2B)[^]
-
Some code segment in chapter 10 of "Windows via C/C++"(5th Edition) is
// Open the source file without buffering & get its size
CEnsureCloseFile hFileSrc = CreateFile(pszFileSrc,
GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_FLAG_NO_BUFFERING |
FILE_FLAG_OVERLAPPED, NULL);Here "CEnsureCloseFile" is a class. The Author define Template class "CEnsureCleanup" which was used to clean some objects, then use a this Template define class CEnsureCloseFile My question is why the HANDLE created by "CreateFile" can be assigned to object hFileSrc . It seems rare. Do you offen do sth like this?
digitalspace.xjtu wrote:
why the HANDLE created by "CreateFile" can be assigned to object hFileSrc .
because the class has an overloaded '=' operator. i don't have the source, but i imagine it looks like this:
TYPE operator=(TYPE t) {
Cleanup();
m_t = (UINT_PTR) t;
return(*this);
}i do stuff like this all the time. i have dozen of little classes designed to take a handle of some kind in the constructor and then call the appropriate release/delete in the destructor.
-
Since it's just a pointer, it can be set to anything that can handle that appropriately (the constructor for this object likely takes in that type of HANDLE). His class probably just makes sure a file is properly closed without requiring you to explicitly close the file (open file handles can be an issue). http://en.wikipedia.org/wiki/Assignment_operator_(C%2B%2B)[^]
thinks , got it
-
digitalspace.xjtu wrote:
why the HANDLE created by "CreateFile" can be assigned to object hFileSrc .
because the class has an overloaded '=' operator. i don't have the source, but i imagine it looks like this:
TYPE operator=(TYPE t) {
Cleanup();
m_t = (UINT_PTR) t;
return(*this);
}i do stuff like this all the time. i have dozen of little classes designed to take a handle of some kind in the constructor and then call the appropriate release/delete in the destructor.
Oh, it is operator overload , thank you very much