如何获取在 main() 函数中构造的 class 的创建对象?
How can I get the created object of the class that is constructed in main() function?
我想获取在main() 函数中构造的class 的对象,并在另一个class 中使用该对象。
这是class我要取的对象:
typedef enum {
DOWN = 1,
LEFT = 2,
UP = 3,
RIGHT = 4
} tWaypointDir;
class Waypoint
{
sf::Texture texture;
sf::Sprite sprite;
public:
float x, y;
int dir;
int next1, next2, next3;
Waypoint(tWaypointDir dir, tRoadTileType type, int row, int col, int idx, int next1, int next2, int next3); // Constructor for the class.
// idx: internal index of the waypoints, next1, 2, 3: next waypoints of the current one.
// if there is only next1, next2 and next3 are -1.
int getNext(); //Get next waypoint randomly
void getPosition(float &x, float &y, float &dir) const { x = this->x; y = this->y; dir = this->dir; }
void setPosition(float x, float y, float dir) { this->x = x; this->y = y; this->dir = dir; }
void draw(sf::RenderWindow *window) {window->draw(sprite);}
};
我在main()函数中创建的对象是:
Waypoint waypoints[] = { //CTL: Road at top-left, HOR: horizontal, etc..
{UP, CTL, 0, 0, 0, 1, -1, -1}, {RIGHT, CTL, 0, 0, 1, 0, -1, -1},
{RIGHT, HOR, 0, 1, 0, 3, -1, -1}, {RIGHT, HOR, 0, 1, 1, 2, -1, -1},
{RIGHT, TTOP, 0, 2, 0, 5, 6, -1}, {DOWN, TTOP, 0, 2, 1, 4, 6, -1},
{RIGHT, TTOP, 0, 2, 2, 4, 5, -1}, {RIGHT, HOR, 0, 3, 0, 8, -1, -1}
};
现在,在 class 汽车中,我想使用 waypoints[] 对象,因为我将汽车朝 waypoints 的方向移动。我在这个 class 中有一个 move() 函数,我在那个部分使用了这个对象。我们不允许在 main() 函数中进行移动。因此,我必须在 class.
中实现它
我在 Waypoint class 上尝试了 Singleton 设计模式,但它在构造函数部分给我错误。我如何在汽车 class 上实现它?
您可以将 Waypoint
class 上的指针或引用作为参数传递给 Car
class 构造函数或方法。 Car
class 可以将此引用或指针存储为成员。然后你可以从 Car
class
访问这个对象
我想获取在main() 函数中构造的class 的对象,并在另一个class 中使用该对象。
这是class我要取的对象:
typedef enum {
DOWN = 1,
LEFT = 2,
UP = 3,
RIGHT = 4
} tWaypointDir;
class Waypoint
{
sf::Texture texture;
sf::Sprite sprite;
public:
float x, y;
int dir;
int next1, next2, next3;
Waypoint(tWaypointDir dir, tRoadTileType type, int row, int col, int idx, int next1, int next2, int next3); // Constructor for the class.
// idx: internal index of the waypoints, next1, 2, 3: next waypoints of the current one.
// if there is only next1, next2 and next3 are -1.
int getNext(); //Get next waypoint randomly
void getPosition(float &x, float &y, float &dir) const { x = this->x; y = this->y; dir = this->dir; }
void setPosition(float x, float y, float dir) { this->x = x; this->y = y; this->dir = dir; }
void draw(sf::RenderWindow *window) {window->draw(sprite);}
};
我在main()函数中创建的对象是:
Waypoint waypoints[] = { //CTL: Road at top-left, HOR: horizontal, etc..
{UP, CTL, 0, 0, 0, 1, -1, -1}, {RIGHT, CTL, 0, 0, 1, 0, -1, -1},
{RIGHT, HOR, 0, 1, 0, 3, -1, -1}, {RIGHT, HOR, 0, 1, 1, 2, -1, -1},
{RIGHT, TTOP, 0, 2, 0, 5, 6, -1}, {DOWN, TTOP, 0, 2, 1, 4, 6, -1},
{RIGHT, TTOP, 0, 2, 2, 4, 5, -1}, {RIGHT, HOR, 0, 3, 0, 8, -1, -1}
};
现在,在 class 汽车中,我想使用 waypoints[] 对象,因为我将汽车朝 waypoints 的方向移动。我在这个 class 中有一个 move() 函数,我在那个部分使用了这个对象。我们不允许在 main() 函数中进行移动。因此,我必须在 class.
中实现它我在 Waypoint class 上尝试了 Singleton 设计模式,但它在构造函数部分给我错误。我如何在汽车 class 上实现它?
您可以将 Waypoint
class 上的指针或引用作为参数传递给 Car
class 构造函数或方法。 Car
class 可以将此引用或指针存储为成员。然后你可以从 Car
class