0

有没有使用以下的替代方法?

class IGraphBuilder;

public ref class Device
{
private:
    IGraphBuilder* pGraphBuilder;

public:
    void Configure()
    {
        pin_ptr<IGraphBuilder*> ppGraphBuilder = &pGraphBuilder;

        HRESULT hr = CoCreateInstance(CLSID_FilterGraph,
            NULL,
            CLSCTX_INPROC,
            IID_IGraphBuilder, (void**)ppGraphBuilder);

reinterpret_cast(ppGraphBuilder) 编译,但如果这对于这种情况是正确的,我有点困惑。

如果这不是 C++/CLI(&NativeMember实际上是指interior_ptr<Type>(NativeMember)),我会简单地使用static_cast<void**>(&pGraphBuilder),但即使正确转换为pin_ptr以下内容也无法编译

pin_ptr<IGraphBuilder*> ppGraphBuilder = &pGraphBuilder;
static_cast<void**>(ppGraphBuilder)

有什么解决方案还是我被迫使用 (void**) 因为 pin_ptr 很奇怪?

4

1 回答 1

1

reinterpret_cast(因此 C 演员)可能不好,尽管由于pin_ptr. 事实上,你必须调用转换运算符 from cli::pin_ptr<IGraphBuilder*>to IGraphBuilder**first (因此来自编译器的抱怨)。

reinterpret_cast<void**>(static_cast<IGraphBuilder**>(ppGraphBuilder))

是正确的。您可能想先引入一个类型的中间变量IGraphBuilder**

pin_ptr<IGraphBuilder*> p = &pGraphBuilder;
IGraphBuilder** ppGraphBuilder = p;

HRESULT hr = CoCreateInstance(CLSID_FilterGraph,
        NULL,
        CLSCTX_INPROC,
        IID_IGraphBuilder, reinterpret_cast<void**>(p));
于 2012-04-24T21:26:13.330 回答