0

これでいい?基本的に、グローバルを参照する単一の関数の呼び出しを、すべてのゲームエンティティとロジックをカプセル化するクラスに置き換えます。以下は、メインで新しいクラスを呼び出す方法です。これに関する一般的なc++の第一人者のコンセンサスは何でしょうか。

class thingy
{
public:
    thingy()
    {
        loop();
    }
    void loop()
    {
        while(true)
        {
            //do stuff

            //if (something)
                //break out
        }
    }
};

int main (int argc, char * const argv[]) 
{
    thingy();
    return 0;
}
4

2 に答える 2

9

game / events / ...ループを含むコンストラクターを持つことは一般的ではありません。そのようなことを行う通常の方法は、コンストラクターを使用してオブジェクトをセットアップし、長い詳細を開始する別のメソッドを提供することです。

このようなもの:

class thingy
{
public:
    thingy()
    {
        // setup the class in a coherent state
    }

    void loop()
    {
        // ...
    }
};

int main (int argc, char * const argv[]) 
{
    thingy theThingy;
    // the main has the opportunity to do something else
    // between the instantiation and the loop
    ...
    theThingy.loop();
    return 0;
}

実際、ほとんどすべてのGUIフレームワークは、そのように動作する「アプリケーション」オブジェクトを提供します。たとえば、Qtフレームワークを取り上げます。

int main(...)
{
    QApplication app(...);
    // ... perform other initialization tasks ...
    return app.exec(); // starts the event loop, typically returns only at the end of the execution
}
于 2012-05-08T21:34:16.793 に答える
7

私はC++の第一人者ではありませんが、代わりに次のようにします。

struct thingy
{
    thingy()
    {
        // set up resources etc
    }

    ~thingy()
    {
        // free up resources
    }

    void loop()
    {
        // do the work here
    }
};

int main(int argc, char *argv[])
{
    thingy thing;
    thing.loop();
    return 0;
}

コンストラクターは、オブジェクトを構築するためのものであり、アプリケーションのロジック全体を処理するためのものではありません。同様に、コンストラクタで取得したリソースは、必要に応じてデストラクタで適切に処理する必要があります。

于 2012-05-08T21:37:22.593 に答える