为什么即使没有使用指针指针作为参数调用函数,这仍然有效?

时间:2013-03-12 16:52:04

标签: c++ pointers bitmap msdn direct2d

在MSDN的代码中,有这样的:

HRESULT DemoApp::LoadBitmapFromFile(
    ID2D1RenderTarget *pRenderTarget,
    IWICImagingFactory *pIWICFactory,
    PCWSTR uri,
    UINT destinationWidth,
    UINT destinationHeight,
    ID2D1Bitmap **ppBitmap
    )

而且:

hr = LoadBitmapFromFile(
                m_pRenderTarget,
                m_pWICFactory,
                L".\\sampleImage.jpg",
                100,
                0,
                &m_pBitmap
                );

当m_pBitmap实际上是指向ID2D1Bitmap对象(ID2D1Bitmap * m_pBitmap)的指针而不是指向ID2D1Bitmap对象的指针时,它是如何工作的?

3 个答案:

答案 0 :(得分:4)

请注意,传递的参数不是m_pBitmap,而是&m_pBitmap

如果m_pBitmapID2D1Bitmap*,那么当您使用&获取地址时,会得到ID2D1BitMap** - 指向指针的指针,就像函数一样预计。

答案 1 :(得分:2)

获取指针的地址使其成为指针的指针。

int i = 0;
int *pi = &i // &i is the address of i (or a pointer to the int i)
&pi // &pi is the address of pi (or a pointer to the pointer to i)

因此,&m_pBitmap指向指针。

答案 2 :(得分:0)

传递指针的地址使得参数指针指向正好类型的指针。