Pointer to an array of pointers, each being able to hold an object

  arrays, c++, dynamic-memory-allocation, pointers

I am struggling with a CSC assignment right now. I can’t understand what this is supposed to mean:

videoGamesArray – a pointer to an array of pointers. Each pointer in the array should be able to point to (hold the memory address of) an individual Video game object.

I have a VideoGameLibrary class, which should hold VideoGame objects, which is another class. However, I’m not sure how I’m supposed to incorporate the pointer to an array of pointers.

VideoGameLibrary class:

class VideoGameLibrary {
    
public:
    VideoGameLibrary** videoGamesArray;
    int maxGames;
    int numGames;
    
    VideoGameLibrary() {
        VideoGameLibrary* createGames(int);
    }
    ~VideoGameLibrary() {
        VideoGameLibrary* destroyGames(VideoGame*);
    }
    
    int getMaxGames();
    int getNumGames();
    
};

VideoGame class:

class VideoGame {
    
public:
    Text* gameTitle;
    Text* gamePlatform;
    int gameYear;
    Text* gameGenre;
    Text* ageRating;
    Text* userRating;
    
    VideoGame() {
        VideoGame* createVideoGame(Text*, Text*, int, Text*, Text*, int);
    }
    ~VideoGame() {
        void destroyVideoGame(VideoGame*);
    }
    
    Text* getVideoGameTitle();
    
};

However, once I’ve created a videoGame object, I am supposed to dynamically allocate it into the VideoGameLibrary class/array of pointers. I also have to find a way to check if the number of maxGames has been hit and if it has, then I create a new VideoGameLibrary and reallocate it with double the size and all of the old objects into the new one. Hopefully this makes sense.

void addVideoGameToArray() {
    Text* title;
    Text* platform;
    Text* genre;
    Text* ageRating;
    int year;
    int userRating;
    char tempStr[200];
    
    cout << endl;
    cout << right << setw(30) << "Video Game TITLE: " << left;
    cin.getline(tempStr, 200);
    title = createText(tempStr);
    
    cout << endl;
    cout << right << setw(30) << "Video Game PLATFORM: " << left;
    cin.getline(tempStr, 200);
    platform = createText(tempStr);
    
    cout << endl;
    cout << right << setw(30) << "Video Game YEAR: " << left;
    cin >> year;
    cin.ignore();
    
    cout << endl;
    cout << right << setw(30) << "Video Game GENRE: " << left;
    cin.getline(tempStr, 200);
    genre = createText(tempStr);
    
    cout << endl;
    cout << right << setw(30) << "Video Game AGE RATING: " << left;
    cin.getline(tempStr, 200);
    ageRating = createText(tempStr);
    
    cout << endl;
    cout << right << setw(30) << "Video Game USER RATING: " << left;
    cin >> userRating;
    cin.ignore();
    cout << endl;
    
    VideoGame* game = createVideoGame(title, platform, year, genre, ageRating, userRating);
    
}

^ This is my function to create a VideoGame object. I’m stuck at adding it to the array.

Source: Windows Questions C++

LEAVE A COMMENT