问题描述
我有几个介面,例如IA,IB,IC等,它们具有共同的性质,例如。现场。我想知道如何重用这些接口的代码(请保存我对COM聚合的答案)。
当前实现如下:
class CA
// ATL specific ...
{
STDMETHODIMP get_Site(...){.. 。}
STDMETHODIMP put_Site(...){...}
}
类BA
// ATL特定...
{
STDMETHODIMP get_Site(...){...}
STDMETHODIMP put_Site(...){...}
}
class CC
// ATL specific ...
{
STDMETHODIMP get_Site(...){...}
STDMETHODIMP put_Site(...){...}
}
我想实现(但不能),如下。
模板< typename T>
class SharedProperties
{
STDMETHODIMP get_Site(...){...}
STDMETHODIMP put_Site(...){...}
}
class CA:
// ATL specific ...
SharedProperties< CA>
{
//属性是继承的,可以从IC
}访问
class BA
// ATL specific ...
SharedProperties< ; CB>
{
//属性是继承的,可以从IB
访问}
class CC
// ATL specific ...
SharedProperties< ; CC>
{
//属性是继承的,可从IA
访问}
我在阅读后遇到了这个想法(),但网站没有一个工作示例,无论我尝试多少,我不能得到它的工作。我仍然得到不能实例化抽象类,因为纯虚函数get_Site和put_Site没有实现(按照第二个片段)。
EDIT
请注意,我使用的是VS2010。以下示例实现:
class ATL_NO_VTABLE CArticle:
public CComObjectRootEx< CComSingleThreadModel> ;,
public CComCoClass< CArticle ,& CLSID_Article>,
public IDispatchImpl< IArticle,& IID_IArticle,& LIBID_GeodeEdiLib,/ * wMajor = * / 1,/ * wMinor = * / 0>
{
public:
CArticle()
{
}
编译器不知道方法 get_Site
和 put_Site
从接口实现方法。您需要从相应的接口继承SharedProperties类模板。这是使 SharedProperties
一个模板的论据。
{
STDMETHOD(get_Site)()= 0;
STDMETHOD(put_Site)()= 0
};
template< typename T>
class Sharedproperties:T
{
public:
STDMETHODIMP get_Site(){return E_NOTIMPL; };
STDMETHODIMP put_Site(){return E_NOTIMPL; };
};
class CAX:public IA
{
STDMETHOD(other)(){return S_OK; }
}
class CA:public Sharedproperties< CAX>
{
public:
CA(){}
};
请注意,类CA不是直接继承自接口IA。
编辑: VS2008类向导为简单的ATL类对象生成这个继承:
class ATL_NO_VTABLE CMyObject:
public CComObjectRootEx< CComMultiThreadModel> ;,
public CComCoClass< CMyObject,& CLSID_MyObject> ;,
IMyObject
其中 IMyObject
是在IDL定义的接口中。所以在ATL上下文中,你只需要替换IMyObject继承:
class ATL_NO_VTABLE CMyObject:
public CComObjectRootEx< CComMultiThreadModel> ,
public CComCoClass< CMyObject,& CLSID_MyObject>,
public Sharedproperties< MyIntermediateClass>
I have several interfaces, e.g. IA, IB, IC, and so on, that share common properties, e.g. Site. I would like to know how to reuse code for these interfaces (please save me the answer on COM aggregation).
Current implementation is as follows:
class CA
// ATL specific...
{
STDMETHODIMP get_Site(...) {...}
STDMETHODIMP put_Site(...) {...}
}
class BA
// ATL specific...
{
STDMETHODIMP get_Site(...) {...}
STDMETHODIMP put_Site(...) {...}
}
class CC
// ATL specific...
{
STDMETHODIMP get_Site(...) {...}
STDMETHODIMP put_Site(...) {...}
}
What I want to achieve (but cannot) is as follows.
template<typename T>
class SharedProperties
{
STDMETHODIMP get_Site(...) {...}
STDMETHODIMP put_Site(...) {...}
}
class CA :
// ATL specific...
SharedProperties<CA>
{
// properties are inherited and are accessible from IC
}
class BA
// ATL specific...
SharedProperties<CB>
{
// properties are inherited and are accessible from IB
}
class CC
// ATL specific...
SharedProperties<CC>
{
// properties are inherited and are accessible from IA
}
I came across this idea after reading up (http://vcfaq.mvps.org/com/7.htm) but the site does not have a working example and no matter how much I tried I could not get it to work. I keep getting "Cannot instantiate abstract class" because the pure virtual functions get_Site and put_Site are not implemented (as per the second snippet).
EDITDo note that I am using VS2010. Sample implementation below:
class ATL_NO_VTABLE CArticle :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CArticle, &CLSID_Article>,
public IDispatchImpl<IArticle, &IID_IArticle, &LIBID_GeodeEdiLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
{
public:
CArticle()
{
}
The compiler does not know that the methods get_Site
and put_Site
implement the methods from the interface. You need to inherit the SharedProperties class template from the corresponding interface. That's the argument to make SharedProperties
a template at all.
interface IA
{
STDMETHOD(get_Site)() = 0;
STDMETHOD(put_Site)() = 0;
};
template<typename T>
class Sharedproperties : T
{
public:
STDMETHODIMP get_Site() { return E_NOTIMPL; };
STDMETHODIMP put_Site() { return E_NOTIMPL; };
};
class CAX : public IA
{
STDMETHOD(other)() { return S_OK; }
}
class CA: public Sharedproperties<CAX>
{
public:
CA() {}
};
Please note that the class CA is not directly inherited from the interface IA.
Edit: The VS2008 class wizard generates this inheritance for a simple ATL class object:
class ATL_NO_VTABLE CMyObject :
public CComObjectRootEx<CComMultiThreadModel>,
public CComCoClass<CMyObject, &CLSID_MyObject>,
IMyObject
where IMyObject
is the in the IDL defined interface. So in ATL context you just need to replace the IMyObject inheritance:
class ATL_NO_VTABLE CMyObject :
public CComObjectRootEx<CComMultiThreadModel>,
public CComCoClass<CMyObject, &CLSID_MyObject>,
public Sharedproperties<MyIntermediateClass>
这篇关于ATL COM - 如何重用接口方法的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!