0

誰かが説明してくれることを期待していたクラスとは少し混乱しています。

ゲームメニューのボタンを作成するために作成しているクラスがあります。4つの変数があります:

int m_x int m_y int m_width int m_height

次に、クラスでレンダリング関数を使用したいのですが、クラスで4つのint変数を使用して、それをクラスの関数に渡す方法がわかりません。

私のクラスは次のようなものです。

class Button
{
private:
    int m_x, m_y;            // coordinates of upper left corner of control
    int m_width, m_height;   // size of control

public:
Button(int x, int y, int width, int height)
{
   m_x = x;
   m_y = y;
   m_width = width;
   m_height = height;
}

void Render(SDL_Surface *source,SDL_Surface *destination,int x, int y)
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;

    SDL_BlitSurface( source, NULL, destination, &offset );
}


} //end class

私が混乱しているのは、で作成された値がどのようにpublic:Button渡されるvoid renderかです。まだ少し混乱しているので、これまでのところ純粋な運があれば、これが正しいかどうかは完全にはわかりません。

4

2 に答える 2

1

複雑なプログラミングプロジェクトに深く入り込む前に、C++の学習に時間を費やすことをお勧めします。

あなたの質問に答えるために、コンストラクター(Button)で初期化された変数はクラスインスタンスの一部です。したがって、これらはを含むすべてのクラスメソッド内で使用できますRender

于 2012-11-01T03:02:52.953 に答える
1

たぶん例が役立つでしょう:

#include <iostream>
class Button
{
private:
    int m_x, m_y;            // coordinates of upper left corner of control
    int m_width, m_height;   // size of control

public:
    Button(int x, int y, int width, int height) :
        //This is initialization list syntax. The other way works,
        //but is almost always inferior.
        m_x(x), m_y(y), m_width(width), m_height(height)
    {
    }

    void MemberFunction()
    {
        std::cout << m_x << '\n';
        std::cout << m_y << '\n';
        //etc... use all the members.
    }
};


int main() {
    //Construct a Button called `button`,
    //passing 10,30,100,500 to the constructor
    Button button(10,30,100,500);
    //Call MemberFunction() on `button`.
    //MemberFunction() implicitly has access
    //to the m_x, m_y, m_width and m_height
    //members of `button`. 
    button.MemberFunction();
}
于 2012-11-01T03:09:52.720 に答える