Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualThe King2

Posted 01 April 2013 - 03:40 AM

Hello,

 

consider this code. Can somebody tell me why the first example doesn't work, but the second does?

 

    void ReceiveMessage(const BaseMessage& message)
    {
        if(message.m_family == BoundingVisualize::family())
        {
            m_bRender = !m_bRender;
        }
        else if(message.m_family == PickObject::family())
        {
            //works
            TranslatePickMessage(message);
        }
    }

    void TranslatePickMessage(BaseMessage message)
    {
        m_pSelected = static_cast<PickObject*>(&message)->pObject;
    }
	void ReceiveMessage(const BaseMessage& message)
	{
		if(message.m_family == BoundingVisualize::family())
		{
			m_bRender = !m_bRender;
		}
		else if(message.m_family == PickObject::family())
		{
                        //doesn't ... wtf?
			BaseMessage msg = message; // msg(message) doesn't work eigther
			m_pSelected = static_cast<PickObject*>(&msg)->pObject;
		}
	}

Since I can't cast the const reference, I need to first make a copy of the message struct. Now in the second example, I first get a warning about msg not being initialized in the cast line, while I clearly make a copy of the const message, then if the line is called, an exception for really using an unitialized variable is thrown. Why? In the first example it passes, though from what I understand, exactly the same thing should happen. msg does indeed get correctly assigned, and this works too:

            BaseMessage msg = message;
            BaseMessage* pMsg = &msg;
            m_pSelected = static_cast<PickObject*>(pMsg)->pObject;

Whats going on here?


#1The King2

Posted 01 April 2013 - 03:40 AM

Hello,

 

consider this code. Can somebody tell me why the first example doesn't work, but the second does?

 

    void ReceiveMessage(const BaseMessage& message)
    {
        if(message.m_family == BoundingVisualize::family())
        {
            m_bRender = !m_bRender;
        }
        else if(message.m_family == PickObject::family())
        {
            //works
            TranslatePickMessage(message);
        }
    }

    void TranslatePickMessage(BaseMessage message)
    {
        m_pSelected = static_cast<PickObject*>(&message)->pObject;
    }
	void ReceiveMessage(const BaseMessage& message)
	{
		if(message.m_family == BoundingVisualize::family())
		{
			m_bRender = !m_bRender;
		}
		else if(message.m_family == PickObject::family())
		{
                        //doesn't ... wtf?
			BaseMessage msg = message; // msg(message) doesn't work eigther
			m_pSelected = static_cast<PickObject*>(&msg)->pObject;
		}
	}

Since I can't cast the const reference, I need to first make a copy of the message struct. Now in the second example, I first get a warning about msg not being initialized in the cast line, while I clearly make a copy of the const message, then if the line is called, an exception for really using an unitialized variable is thrown. Why? In the first example it passes, though from what I understand, exactly the same thing should happen. msg does indeed get correctly assigned, and this works too:

 

 

            BaseMessage msg = message;
            BaseMessage* mesg = &msg;
            m_pSelected = static_cast<PickObject*>(&mesg)->pObject;

Whats going on here?


PARTNERS