1

我需要为指向类的指针向量构建一个构造函数......

我的课是:

class Song {

    string song_name;

    string auther_name;

    int popularity;

    SongStructure song_format;

    int song_length;

    int lengths[size];

    bool first_condition_true();

    bool second_condition_true(int index);

    }

};

我的向量是:

vector<Song*> play_list;
4

2 回答 2

2

随着新标准 C++11 / 0x 初始化列表的引入:

我假设你想创建一个 SongBook 类,其中包含一个歌曲指针向量,然后是附加信息。它们可以这样使用:

类文件:

class SongBook {
    vector<Song*> songlist;
    string name;

    // Constructor
    SongBook(std::initializer_list<Song*> songs) : songlist(songs) {}
}

然后像这样从你的 main 调用例如

SongBook book({new Song(...), new Song(...), new Song(...)});
于 2013-02-01T14:09:31.453 回答
0

您的向量有一个可以使用的有效构造函数。(它为指针创建位置)。您所要求的可能是一种使指针指向某个有效的方法Song。您将需要在某个循环中执行此操作(可能是 STL“循环”)。但你可能不需要那个,因为你的歌曲是合法可复制的!?。您可以使用vector<Song>

于 2013-02-01T14:10:53.993 回答