Thread: Understanding this piece of code

  1. #1
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33

    Understanding this piece of code

    Code:
    //Code from my SystemClass!
    
    bool SystemClass::Frame()
    {
        bool result;
    
    
        //check if the user pressed escape and wants to exit the application.
        if (m_Input->IsKeyDown(VK_ESCAPE))
        {
            return false;
        }
    }
    
    //Code from my InputClass!
    void InputClass::KeyDown(unsigned int input)
    {
        // If a key is pressed then save that state in the key array.
        m_keys[input] = true;
        return;
    }
    I do not understand why my Frame function in my SystemClass is returning false. It is checking too see if the escape button is pressed, then why return false? Also what is returning false in this method...is it the m_Input?

    I am a little confused, could someone please clarify
    "Don't quit. Suffer now and live the rest of your life as a champion"
    - Muhammad Ali


  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    if the expression

    Code:
    m_Input->IsKeyDown(VK_ESCAPE)
    is not true then what value does bool SystemClass::Frame() return?

    Is there a return statement at the end of the function?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    As Hodor says, if that condition is false, there is no return statement at the end of the function. This still compiles, because sometimes you don't want to generate code for every input of the function if you know that the function will only ever be called with a narrower set of inputs than allowed by the type system. However, it is undefined behavior to call the function with parameters that reach the end of a non-Void function without returning or throwing. So the compiler can assume that the function is only ever called with valid parameters, and optimise the function based on that assumption.

    Some compilers will generate a predictable crash when this code is compiled in debug mode. But if you turn up optimisation, the Frame function will be optimized to always return false.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I fix this piece of code?
    By deepee in forum C Programming
    Replies: 32
    Last Post: 12-04-2013, 08:06 PM
  2. I am not understanding a piece of code
    By straygrey in forum C Programming
    Replies: 2
    Last Post: 11-07-2013, 06:39 AM
  3. Need help understanding a piece of code
    By youjustreadthis in forum C Programming
    Replies: 14
    Last Post: 07-06-2012, 08:42 AM
  4. Help on understanding a piece of code
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2011, 10:55 AM
  5. Help with a piece of code
    By Victor4015 in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2005, 05:38 PM