指向一组指针的指针,每个指针都可以容纳一个对象

时间:2021-02-23 02:25:22

标签: c++ arrays pointers dynamic-memory-allocation

我现在正在为 CSC 任务苦苦挣扎。我不明白这是什么意思:

videoGamesArray – 一个指向指针数组的指针。数组中的每个指针都应该能够指向(保存其内存地址)一个单独的视频游戏对象。

我有一个 VideoGameLibrary 类,它应该保存 VideoGame 对象,这是另一个类。但是,我不确定我应该如何将指针合并到一个指针数组中。

VideoGameLibrary 类:

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

视频游戏类:

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

但是,一旦我创建了一个 videoGame 对象,我就应该将它动态分配到 VideoGameLibrary 类/指针数组中。我还必须找到一种方法来检查 maxGames 的数量是否已达到,如果已达到,然后我创建一个新的 VideoGameLibrary 并将其重新分配为两倍大小,并将所有旧对象重新分配到新对象中。希望这是有道理的。

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

^ 这是我创建 VideoGame 对象的函数。我一直坚持将它添加到数组中。

0 个答案:

没有答案