c++windowsdirect2d

How Do I Determine Whether to Use '->' versus '.'


I'm following a D2D sample and therein, this code came up:

    if(!(D2D1_WINDOW_STATE_OCCLUDED & m_renderTarget->CheckWindowState()))
    {
        m_renderTarget->BeginDraw();

        Draw();

        if(m_renderTarget->EndDraw() == D2DERR_RECREATE_TARGET)
        {
            m_renderTarget.Reset();
            Invalidate();
        }
    }

I noticed that the m_renderTarget used both the -> (I forget what it's called) and later uses the dot operator. For some reason I thought that use could only use one or the other depending of if it was a reference type or value type. Apparently I thought wrong but can't find any information about when to use each operator (or more to the point, the purpose of each operator.)

Any clarification would be greatly appreciated.


Solution

  • I thought that use could only use one or the other depending of if it was a reference type or value type

    Usually, yes.

    The fact that both are used on the same object in your code indicates it is a "smart pointer" i.e. an object type which has an overloaded operator-> that allows it to act like a pointer.

        m_renderTarget->BeginDraw();
    

    This uses the operator-> to access a member of the object it points to. BeginDraw is a member function of the pointed-to object, not of m_renderTarget.

        m_renderTarget.Reset();
    

    This accesses a member of m_renderTarget itself, not the object it points to. Typically a reset() member replaces the pointed-to object with a null pointer.

    So in the first case the -> syntax does something with the object it points to, and in the second case the . syntax does something to the object itself.