samedi 6 juin 2020

How can I get the created object of the class that is constructed in main() function?

I want to get the object of the class which is constructed in main() function and use this object in another class.

This is the class I want to take the object of it:

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);}
};

The object I created in the main() function is:

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}
};

Now, in the class Car, I want to use the waypoints[] object because I move the car in the direction of waypoints. I have a move() function in this class and I used this object in that part. We are not allowed to do the movement in main() function. Hence, I have to implement this in the class.

I tried the Singleton design pattern on the Waypoint class but it gives me errors on the constructor part. How can I implement this on the Car class?

Aucun commentaire:

Enregistrer un commentaire