2012-04-02 2 views
1

OK 일부 게임 논리에 대해 연구 중이므로 인터넷에서 허용하는만큼 많은 연구를 수행했지만 여전히 클래스 및 구조체에 대한 확실한 이해가 없으므로 부드럽게 진행하십시오!클래스 또는 구조체 내부의 연산자 사용?

기본적으로 한 줄에 모든 속성을 가진 개체를 만들 수 있습니다.

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

내가 몇 가지 여분의 물건이 aswell처럼 될하려면 :

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

내가 작업중인 어떤 언어 모르겠지만,이 C++처럼 조금 보이는, 그래서 여기에 예 :

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

관련 문제