-2

I'll go straight to an example, I think it is easier to underestand. Music Cd has Tracks. How can I access A TrackInfo vector (XTrackInfo) data "inside" Music Cd class? I want to print and even change values, I don't figure out how. Thanks

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

#include <iterator>
#include <numeric>



class XTrackInfo
{
    std::string m_TrackName;
    int m_Length;

public:
    XTrackInfo() {}

    XTrackInfo(std::string TrackName, int Length):
        m_TrackName(std::move(TrackName)),
        m_Length(Length)
    {}

    void SetTrackName(std::string TrackName) { m_TrackName = std::move(TrackName); }
    void SetTrackLength(int Length) { m_Length = Length; }
    const std::string& GetTrackName() const { return m_TrackName; }
    int GetTrackLength() const { return m_Length; }
};

class XMusicCd
{
private:

    std::string m_Author;
    std::vector<XTrackInfo> m_TrackInfo;

public:

    XMusicCd() {}
    XMusicCd(std::string Author, std::vector<XTrackInfo> Tracks):
        m_Author(std::move(Author)),
        m_TrackInfo(std::move(Tracks))
    {}

    void SetAuthor(std::string Author) { m_Author = std::move(Author); }

    const std::string& GetAuthor() const { return m_Author; }
    const std::vector<XTrackInfo> GetTracks() const { return m_TrackInfo;}

    int GetLength() const; // Left incomplete on purpose; you will implement it later


    void AddTrack(XTrackInfo NewTrack){

        m_TrackInfo.emplace_back(std::move(NewTrack));

    }


};



void PrintCdContents(const XMusicCd& Cd)
{

    std::cout << "Author : " << Cd.GetAuthor() << "\n";
    std::cout << "\n" << std::endl;
    std::cout << "Track Info" << std::endl;

   //problems here :)


}

int main()
{
    // You may not change this function
    XMusicCd MyCd;
    MyCd.SetAuthor("Hello World");
    MyCd.AddTrack(XTrackInfo("This is a test", 100));
    MyCd.AddTrack(XTrackInfo("This is a test 2", 200));
    PrintCdContents(MyCd);
}
marco
  • 915
  • 4
  • 17
  • 35
  • Is this a homework question? You can use the public interface of XTrackInfo inside your XMusicCd method, e.g. call GetTrackName(). – Jens Sep 03 '14 at 16:11
  • It is not so obvious as you can see in @Messa answer. – marco Sep 03 '14 at 16:23
  • 1
    The answer is still obvious, but the problem is not :-) It would have been easier if you had written a very small example with some line of code saying "here I want to do this or that, but I don't know how". Just syaing "How can I access" was not clear to me. – Jens Sep 03 '14 at 16:48
  • Yes, then you're right ! There is smarter ways to asking :) However, looking into the only answer with code... Do I really need create a vstd::vector ? – marco Sep 03 '14 at 19:32
  • Depends on where you want to access the tracks. You can access the data member directly inside XMusicCd, but from outside the class you have to use the accessor method GetTracks. You could prevent the copy when you return a const& instead of a copy. – Jens Sep 03 '14 at 21:07

1 Answers1

1

Use iterators:

std::vector<XTrackInfo> tracks = Cd.GetTracks();
for (std::vector<XTrackInfo>::const_iterator it = tracks.begin(); it != tracks.end(); ++it) {
    std::cout << it->GetTrackName() << std::endl;
}

Or indexes:

std::vector<XTrackInfo> tracks = Cd.GetTracks();
for (unsigned i = 0; i < tracks.size(); ++i) {
    std::cout << tracks.at(i).GetTrackName() << std::endl;
}
Messa
  • 24,321
  • 6
  • 68
  • 92
  • I've tried use for loops with auto keywork. Do I really need to create another vector, tracks ? I think this seems quite "newbie" answers, but I have never used stl before. – marco Sep 03 '14 at 16:25
  • 1
    GetTracks returns a copy already. You could change it to return reference. See http://stackoverflow.com/questions/2182408/return-a-const-reference-or-a-copy-in-a-getter-function – Messa Sep 03 '14 at 23:58