一个臭虫???在C ++中实现IUnknown [英] A Bug ??? Implementing IUnknown in C++
问题描述
您好,全部:
请参阅: < a href ="http://msdn.microsoft.com/en-us/library/office/cc839627.aspx"style ="font-size:0.75em; line-height:1.5"> http://msdn.microsoft .com / zh-CN / library / office / cc839627.aspx
我想有一个错误:
InterlockedIncrement(m_cRef);
应该是
InterlockedIncrement(& m_cRef);
和
< pre class ="prettyprint"> ULONG ulRefCount = InterlockedDecrement(m_cRef);
应该是
ULONG ulRefCount = InterlockedDecrement(& m_cRef);
请确认......
干杯
Pei
Ye s,这是一个错字。
虽然技术上很有趣,因为没有显示m_cRef的声明,这个特殊的例子仍然可能是通过假设m_cRef是使用支持隐式
的运算符来实现运行符号 volatile unsigned long * 和 ULONG 之类的这个:#include< windows.h>
class MyRefCount
{
public:
MyRefCount():n(1){}
operator volatile unsigned long *()
{
return& n;
}
运营商ULONG()
{
return n;
}
私人:
volatile ULONG n;
};
class CMyMAPIObject
{
public:
ULONG Release();
private:
MyRefCount m_cRef;
};
ULONG CMyMAPIObject :: Release()
{
//减少对象的内部计数器。
ULONG ulRefCount = InterlockedDecrement(m_cRef);
if(0 == m_cRef)
{
删除此;
}
返回ulRefCount;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR sCmdLine,int nCmdShow)
{
CMyMAPIObject * m = new CMyMAPIObject;
m-> Release();
返回0;
}但这只是一个C ++特技。   ;; - )
我认为你最有意义的是它是一个错字。
Hi, all:
Please refer to : http://msdn.microsoft.com/en-us/library/office/cc839627.aspx
I guess there is a bug:
InterlockedIncrement(m_cRef);
should be
InterlockedIncrement(&m_cRef);
and
ULONG ulRefCount = InterlockedDecrement(m_cRef);
should be
ULONG ulRefCount = InterlockedDecrement(&m_cRef);
Please confirm...
Cheers
Pei
Yes, it is a typo.
Although it's amusing that technically, because the declaration of m_cRef is not shown, this particular example could still be correct (with a little tricky thinking alone) by assuming that m_cRef is implemented with a class that supports implicit cast operators to both volatile unsigned long* and ULONG like this:
#include <windows.h> class MyRefCount { public: MyRefCount() : n(1) {} operator volatile unsigned long*() { return &n; } operator ULONG() { return n; } private: volatile ULONG n; }; class CMyMAPIObject { public: ULONG Release(); private: MyRefCount m_cRef; }; ULONG CMyMAPIObject::Release() { // Decrement the object's internal counter. ULONG ulRefCount = InterlockedDecrement(m_cRef); if (0 == m_cRef) { delete this; } return ulRefCount; } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR sCmdLine, int nCmdShow ) { CMyMAPIObject* m = new CMyMAPIObject; m->Release(); return 0; }But that's just a C++ stunt. ;-)
I think that it makes the most sense that you're right about it being a typo.
这篇关于一个臭虫???在C ++中实现IUnknown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!