Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
#include <Windows.h>

class App;

class System
{
public:
    System(App * app) : mApp(app) { }
    bool update();
    App * mApp;
};

class App
{
public:
    App(void) : mExit(false) { }

    void setSystem(System * s) { mSystem = s; }

    void run(void)
    {
        while (!mExit)
        {
            mExit = mExit || !mSystem->update();
            ::Sleep(500);
        }
    }

    System * mSystem;
    bool mExit;
};

bool System::update()
{
    mApp->mExit = true;
    return true;
}

int main()
{
    App a;
    System s(&a);
    a.setSystem(&s);

    a.run();

    return 0;
}
Posted

Why would it terminate ? The value of mExit starts as false and remains so forever. (update() always returns true).
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 24-Mar-12 18:26pm    
Of course; a 5.
--SA
I see that your intention was to set mExit to true in System::update. In fact mExit gets set to true. But when the update function returns mExit is overwritten once again with the result of the expression

mExit || !mSystem->update()


The clue is that the mExit variable of this expression has already been evaluated before update() gets called. So the result of the expression is false and mExit is set to false again.

I would recommend that you update the mExit variable only in the System::update() function and rewrite the loop to

while (!mExit)
 {
     mSystem->update();
     ::Sleep(500);
 }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900