你好我想知道为什么const
在这个类的“get”函数中使用?
class Album {
public:
Album(string c, Artist* a, string t, int y, int no):barCode(c), artist(a), title(t), year(y), noCopiesSold(no){}
const string& getBarCode() const {return barCode;}
Artist* getArtist() const {return artist;}
const string& getTitle() const {return title;}
int getYear() const {return year;}
int getNoCopiesSold() const {return noCopiesSold;}
void setBarCode(string value) {barCode = value;}
void setArtist(Artist* value) {artist = value;}
void setTitle(string value) {title = value;}
void setYear(int value) {year = value;}
void setNoCopiesSold(int value) {noCopiesSold = value;}
friend ostream& operator << (ostream& out, const Album& album)
{
out << album.artist->getName() << " " << album.barCode << " " << album.title << " " << album.year << " " << album.noCopiesSold << endl;
return out;
}
private:
string barCode;
Artist* artist;
string title;
int year;
int noCopiesSold;
};