-4

My ClassPlaylistTrackTrack(扩展该类)的子类:

 #include "track.h"
 #include "album.h"

 class PlaylistTrack : public Track //extends Track Class
 {
   private:
           Album & newAlbum; //or
           Album * newAlbum; //?


   public:
       PlaylistTrack(void);
      ~PlaylistTrack(void);

 };

我希望我有正确的语法。您如何为出现特定曲目的专辑添加引用或指针?

4

2 回答 2

2

我不太明白你在问什么,但听起来你想有一种方法从 PlaylistTrack 中获取专辑。

class PlaylistTrack : public Track
{
 public:
     PlaylistTrack(Album * owner){ m_owner = owner; }
     Album* getAlbum(){return m_owner;}
 private:
     Album* m_owner;

}

int main()
{

Album albumA;
PlaylistTrack newTrack(&albumA);

//Now the track knows what album it belongs to, but the album does not own the track    yet.
std::cout << "New Track's Album: " << newTrack.getAlbum.getTitle() << std::endl;

//Now the album owns this track
albumA.addTrack(newTrack);

//The PlaylistTrack constructor could add itself to the album if you wanted to I think.

return 0;
}
于 2012-12-11T17:56:47.893 回答
0

添加这样的引用:

class PlaylistTrack : public Track //extends Track Class
{
   private:
       Album &album;
   public:
       PlaylistTrack(void);
      ~PlaylistTrack(void);

};

或这样的指针:

class PlaylistTrack : public Track //extends Track Class
{
   private:
       Album *album;
   public:
       PlaylistTrack(void);
      ~PlaylistTrack(void);

};

当然,您可能想要修改您的构造函数,以便它采用专辑和曲目编号(或类似的东西),或者至少为专辑和曲目编号添加访问器。

于 2012-12-11T17:56:49.313 回答