locked
Can someone explain to me whats going on here. RRS feed

  • Question

  • BOOL OGLWindow::MouseMove(int x, int y)
    {
    Listener *plistener = static_cast<Listener*>(m_rect);
    plistener->MouseMove(x - m_width * 0.5, m_height * 0.5 - y);

    return TRUE;
    }
    Monday, November 24, 2014 2:29 PM

Answers

  • Well, without knowing more about your inheritence heiarchy and the declaration of m_rect, I can only make some general statements.

    Your OGLWindow class has a m_rect member variable.  That variable is not of type Listener*.  static_cast<Listener*> is used to cast m_rect to a pointer to Listener.  The compiler will prevent a nonsensical cast. For example,  if m_rect is something that couldn't be a Listener* that would be a compiler error.  Also, if m_rect were a const pointer, static_cast would prevent the cast from removing the const.  However, no runtime check will be performed, so you better be sure that m_rect really is a Listener.

    Once the cast is made, a method is called on the Listener object.

    class ListenerBase {};
    class Listener : public ListenerBase{};
    class Hearer :  public ListenerBase{};
    class Other {};
    
    ListenerBase *lb = new Listener();
    Listener *l = static_cast<Listener*>(lb); // valid cast
    Hearer *h = static_cast<Hearer*>(lb); // No compiler error, but undefined behavior.  lb is not a Hearer.
    Other *o = static_cast<Other*>(lb); // compiler error.  No conversion from ListenerBase to Other
    
    
    Also look up dynamic_cast while you are learning about this.

    • Marked as answer by Shu 2017 Tuesday, December 2, 2014 9:11 AM
    Monday, November 24, 2014 2:50 PM

All replies

  • BOOL OGLWindow::MouseMove(int x, int y)
    {
    Listener *plistener = static_cast<Listener*>(m_rect);
    plistener->MouseMove(x - m_width * 0.5, m_height * 0.5 - y);

    return TRUE;
    }

    Please do not put the whole question in the subject.

    But what is your question really? How can we answer without some information about the type of m_rect and the definition of thListener class?

    I would say, though, that the cast looks rather odd.


    David Wilkinson | Visual C++ MVP

    Monday, November 24, 2014 2:46 PM
  • Code is moving cursor to center of the rectangle.  Always returns true.

    jdweng

    Monday, November 24, 2014 2:49 PM
  • Well, without knowing more about your inheritence heiarchy and the declaration of m_rect, I can only make some general statements.

    Your OGLWindow class has a m_rect member variable.  That variable is not of type Listener*.  static_cast<Listener*> is used to cast m_rect to a pointer to Listener.  The compiler will prevent a nonsensical cast. For example,  if m_rect is something that couldn't be a Listener* that would be a compiler error.  Also, if m_rect were a const pointer, static_cast would prevent the cast from removing the const.  However, no runtime check will be performed, so you better be sure that m_rect really is a Listener.

    Once the cast is made, a method is called on the Listener object.

    class ListenerBase {};
    class Listener : public ListenerBase{};
    class Hearer :  public ListenerBase{};
    class Other {};
    
    ListenerBase *lb = new Listener();
    Listener *l = static_cast<Listener*>(lb); // valid cast
    Hearer *h = static_cast<Hearer*>(lb); // No compiler error, but undefined behavior.  lb is not a Hearer.
    Other *o = static_cast<Other*>(lb); // compiler error.  No conversion from ListenerBase to Other
    
    
    Also look up dynamic_cast while you are learning about this.

    • Marked as answer by Shu 2017 Tuesday, December 2, 2014 9:11 AM
    Monday, November 24, 2014 2:50 PM