2012-04-02 7 views
1

OK私はいくつかのゲームロジックに取り組んでいますので、私はかなりの研究(インターネットが許す限り)を行いましたが、クラスと構造体についての十分な理解はまだまだありません。クラスまたは構造体内の演算子を使用していますか?

基本的には、すべてのプロパティを持つオブジェクトを1行に作成することができます。

object a{1, 1, 50, 15, 5}; // create object a 

と私はいくつかの余分なものがaswellなどを作ることがしたい:私はあなたが作業しているどの言語か分からないが、それは少しC++のように見えるので、ここだ

class object 
{ 
public: 
int x; 
int y; 
int h; 
int w; 
int s; 
int x1; 
int y1; 
int ps; 
int ns; 
int x1 = x + w; 
int y1 = y + h; 
int ps = 0 + s; 
int ns = 0 - s; 
}; 
+0

お探しの情報がわかるよう、具体的な質問をしてください。また、どの言語、プラットフォームを使用していますか? – Khan

答えて

0

例:

class Rect 
{ 
    public: 
     int x, y; 
     int w, h; 
     int right, bottom; 

     // This method is called a constructor. 
     // It allows you to perform tasks on 
     // the instantiation of an object. 
     Rect(int x_, int y_, int w_, int h_) 
     { 
      // store geometry 
      this->x = x_; 
      this->y = y_; 
      this->w = w_; 
      this->h = h_; 

      // calculate sides 
      this->right = x_ + w_; 
      this->bottom = y_ + h_; 
     } 
}; 

// You use the constructor in your main() function like so: 
Rect myObject(1, 1, 50, 15); 

// And you can access the members like so: 
myObject.x = 10; 
myObject.right = myObject.x + myObject.w; 

質問で提案したように、クラス定義内で演算子を使用することはできません。変数に対する操作は、コンストラクタ(または他のメソッド)内で行わなければなりません。

+0

それは私にとって非常に有益なことでした。 – user1308858

関連する問題