Programming question of the week ;)
Mikael Hansson
EMAIL HIDDEN
Tue Apr 14 21:41:05 CEST 2009
Suppose you have this:
class Item {
protected:
string title;
public:
Item(string t) : title(t) {}
};
class Cd : public Item {
private:
string artist;
public:
Cd(string a, string t) : Item(t), artist(a) {}
void setArtist(string a) {artist = a}
};
int main()
{
Cd *cd1 = new Cd("Music-barr", "Shelter");
vector<Item*> vec;
vec.push_back(cd1);
vec[0]->setArtist("Music-bar");
}
That last line is the problem since the CD* becomes a Item* once it is
stored in the vector and hence the setArtist function isn't recognized.
How do you solve this? It doesn't make sense to do virtual functions in
class Item for every possible set-get function in the children classes.
Totally wrong approach?
/Micke
More information about the music-bar
mailing list