Programming question of the week ;)

M-.-n EMAIL HIDDEN
Wed Apr 15 09:59:03 CEST 2009


Another possibility, but that I save only for extreme case (because it's 
not super elegant) is to have a placeholder for a type field in your 
base class:

Enum ItemType {
    IT_BOOK,
    IT_CD
} ;

Have the parameter to be mandatory in the item constructor so you can do

int main()
{
   Cd *cd1 = new Cd("Music-barr", "Shelter");

   vector<Item*> vec;
   vec.push_back(cd1);

   if (vec[0]->GetType()==IT_CD) {
	Cd *cd=(Cd *)vec[o] ;
	cd->setArtist("Music-bar");
   }

}


You can achieve the same using dynamic casts but for some reason I never 
really like it's syntax.

Mikael Hansson a écrit :
> Martin Naef skrev:
>   
>> I'm with Marc on that one. Generally, do the setArtist thing while you 
>> know what type you're dealing with. You can either do that by keeping 
>> separate lists for each subtype, or by having your editing functionality 
>> through a type-aware editing system.
>>   
>>     
>
> Soundds like good ideas, I'll save this in my valuable strategic design 
> compartment for when I'm doing a *real* program :)
> I actually don't need the functionality I tried to get in order to make 
> the application work. I think that one part of really understanding 
> programming is to try many things and then get your head around why some 
> of them doesn't work and what would be a better choice of design.
>
> Thanks for helping out!
>
> /Micke
>
>
> ps. let me know when I get too annoying, it's just that it's great 
> getting input from people that actually know what they're talking about
> _______________________________________________
> music-bar mailing list
> music-bar at lists.music-bar.org
> http://lists.music-bar.org/cgi-bin/mailman/listinfo/music-bar
>
>   




More information about the music-bar mailing list