C++ IUnknown , C++ (runtime layer) . IUnknown , . C++, C++ , , , .
Visual Basic Java, C++, QueryInterface, AddRef Release. IUnknown . Java QueryInterface :
public void TryToSnoreAndIgnore(Object obj) { IPug pug; try { pug = (IPug)obj; // VM calls QueryInterface // VM QueryInterface pug.Snore(); } catch (Throwable ex) { // ignore method or QI failures // QI } ICat cat; try { cat = (ICat)obj; // VM calls QueryInterface // VM QueryInterface cat.IgnoreMaster(); } catch (Throwable ex) { // ignore method or QI failures // QI } }
Visual Basic . , , (VM) Visual Basic QueryInterface :
Sub TryToSnoreAndIgnore(obj as Object) On Error Resume Next ' ignore errors ' Dim pug as IPug Set pug = obj ' VM calls QueryInterface ' VM QueryInterface If Not (pug is Nothing) Then pug.Snore End if Dim cat as ICat Set cat = obj ' VM calls QueryInterface ' VM QueryInterface If Not (cat is Nothing) Then cat.IgnoreMaster End if End Sub
, Java, Visual Basic, QueryInterface . AddRef Release, .
, C++, , . (raw) IUnknown. :
. - , , , . Visual C++ 5.0, , ( MSC, ATL, Direct-to-COM), , . 1995 1996 "C++ Report" , 1. , , - , . , , . , SmartInterface, (template) : C++ IID. IUnknown :
#include "smartif.h" void TryToSnoreAndIgnore(/* [in] */ IUnknown *pUnk) { // copy constructor calls QueryInterface // QueryInterface SmartInterface<IPug, &IID_IPug> pPug = pUnk; if (pPug) // typecast operator returns null-ness // pPug->Snore(); // operator-> returns safe raw ptr // -> // copy constructor calls QueryInterface // QueryInterface SmartInterface<ICat, &IID_ICat> pCat = pUnk; if (pCat) // typecast operator returns null-ness // pCat->IgnoreMaster(); // operator-> returns safe raw ptr // -> // destructors release held pointers on leaving scope // // }
, , ; , , . , ; , . , ->. , Release - , Release .
1 http:/www.develop.com/dbox/cxx/InterfacePtr.htm http://www.develop.com/dbox/cxx/SmartPtr.htm.