請考慮以下代碼。綁定到本地引用的對象是否自動被破壞?
struct MyImage
{
MyImage(const int handle);
MyImage(const CString& filePath);
virtual ~MyImage();
void Process();
void SaveAs(const CString& filePath);
// No copying!
MyImage(const MyImage& other) = delete;
MyImage& operator=(const MyImage& other) = delete;
}
void ProcessImageFile(const CString& inFilePath, const CString& outFilePath)
{
MyImage& image = MyImage(-1); // initialized with invalid handle
if (DecryptionRequired())
{
const CString tempFilePath = ::GetTempFileName();
Decrypt(inFilePath, tempFilePath);
image = MyImage(tempFilePath);
_tremove(tempFilePath);
}
else
{
image = MyImage(inFilePath);
}
image.Process();
image.SaveAs(outFilePath);
}
將image
引用的對象時ProcessImageFile()
回報破壞?
注意,該代碼是在標準C++無效的,所以這是一個Visual-C++具體的問題。 – Brian
@Brian只是爲了好奇,哪部分是非標準的? – user463035818
@ tobi303將一個非常量左值引用綁定到一個右值 – Brian