Programming question of the week ;)

cyrille.damez EMAIL HIDDEN
Wed Apr 15 13:57:57 CEST 2009


Actually, you don't have to use an additional enum if you don't switch off RTTI in your compiler option (http://en.wikipedia.org/wiki/Runtime_Type_Information) because the type information is already embedded in the object.
With RTTI, you can simply dynamic_cast to the derived type, and if it fails (returns NULL) then you have the wrong type:

so instead of:

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

you would have:

Cd*cd = dynamic_cast<Cd*>(vec[0]);
if(cd) cd->setArtist("Music-bar");



 > Message du 15/04/09 09:59
> De : "M-.-n"
> A : "Music-bar"
> Copie à :
> Objet : Re: Programming question of the week ;)
>
>
>
> 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 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
> >
> >
>
> _______________________________________________
> music-bar mailing list
> music-bar at lists.music-bar.org
> http://lists.music-bar.org/cgi-bin/mailman/listinfo/music-bar
>
>  

 Créez votre adresse électronique prenom.nom at laposte.net 
 1 Go d'espace de stockage, anti-spam et anti-virus intégrés.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.music-bar.org/pipermail/music-bar/attachments/20090415/1e2e7118/attachment-0001.html>


More information about the music-bar mailing list