0

- - - - - - - - - -質問1: - - - - - - - - - - - -

私はSceneクラスを持っていて、...まあ...「シーン」と呼ばれるシーンの配列を作りたいです:

class Scene
{
public:
    int id;
    string title;
    Image backgroundImage;
    Scene( int id, string title, Image backgroundImage );
};

ゲーム ヘッダーの Game クラス内でシーン配列を宣言します。

Scene scenes[ 2 ];

次に、game.cpp ループ デ ループ内のシーンでポンピングを開始します。

scenes[ 0 ] = Scene();

新しいシーンを宣言せずに上記を実行できるのはなぜですか? 例えば:

scenes[ 0 ] = new Scene();

クラス Scene を public として宣言していないためでしょうか。それは静的または何かとして作成されますか?私は混乱しているスクービー!

- - - - - - - - - - 質問2 - - - - - - - - - - - -

シーンのプロパティをコンストラクターに渡すより良い方法はありますか...たとえば、javascript では次のようにします。

var Scene = function( properties )
{
    this.id = properties.id;
    this.string = properties.string;
    this.backgroundImage = properties.backgroundImage;
}

var scenes = [
    new Scene( { 
        id:0, 
        string:"a scene", 
        Image: new Image( ... ) 
    } ),        
    new Scene( { 
        id:1, 
        string:"a scene 1", 
        Image: new Image( ... ) 
    } ),
]

これは自己文書化になります..私のドリフトブラを捕まえますか?

- - - - - - ノート - - - - - - :

Scene[ 0 ] = Scene() と言うだけでオブジェクトの新しいインスタンスが宣言されることを知らなかったので、それを new として宣言する必要があると思いますか?

4

5 に答える 5

1
Scene scenes[2];

これにより、2 つのオブジェクトの配列が作成されSceneます。オブジェクトへのポインターではありません。オブジェクト。各オブジェクトは、デフォルトのコンストラクターで初期化されます。

他のコンストラクターによって初期化されたオブジェクトで配列を作成するには、次のようにします。

Scene scenes[2] = {
  Scene(0, "a scene", Image(...)),
  Scene(1, "a scene 1, Image(...)) };
于 2013-03-27T22:11:28.947 に答える
1
#include <string>
using namespace std;

struct Image {};

class Scene
{
private:
    int id_;
    string title_;
    Image backgroundImage_;
public:
    Scene( int id, string const& title, Image const& backgroundImage )
        : id_( id )
        , title_( title )
        , backgroundImage_( backgroundImage )
    {}
};

#include <iostream>
int main()
{
    Scene scenes[] = {
        Scene( 0, "a scene", Image()  ),
        Scene( 1, "a scene 1", Image()  )
        };
    // Whatever.
}
于 2013-03-27T22:14:18.933 に答える
1
  1. JavaScript ではnewオブジェクトを作成しますが、C++ では新しく作成されたオブジェクトへのポインターを返します。そうすることscenes[0] = Scenes()は正しいです。

  2. 多分あなたは試すことができますstd::vector

    #include <vector>
    
    std::vector<Scenes> scenes{
        Scenes{0, "a scene", Image{}},
        Scenes{1, "a scene1", Image{}},
    };
    
于 2013-03-27T22:15:23.610 に答える
0

あなたはこれを求めている:

class Scene
{
    int id;
    string title;
    Image backgroundImage;

public:
    Scene(int id, string const & title, Image const & backgroundImage)
    : id(id)
    , title(title)
    , backgroundImage(backgroundImage)
    { }
};

Scene scenes[2] = { Scene(1, "me", image1), Scene(2, "you", image2) };

最新のコンパイラ (C++11) を使用すると、次のように書くこともできます。

Scene scenes[2] = { {1, "me", image1}, {2, "you", image2} };

そして、あなたはおそらく好むでしょうstd::array<Scene, 2>

于 2013-03-27T22:10:16.767 に答える
0

最初の質問:

Scene scenes[ 2 ]; /declares an array of Scene objects
scenes[ 0 ] = Scene();
How comes I can do the above without having to declare a new Scene? for example:

scenes[ 0 ] = new Scene();
Is it because I did not declare the class Scene as public? 

new演算子はオブジェクトを作成し、pointerクラスのオブジェクトに a を返します。を使用する場合はnew、配列を へのポインタの配列として宣言する必要がありますScene。オブジェクトの配列の場合、それらのオブジェクトを初期化するために呼び出すコンストラクターを指定しない場合、デフォルトのコンストラクターが呼び出されます。

于 2013-03-27T22:11:42.993 に答える