[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Nedarving




"Martin E. Nielsen" wrote:

> Hejsa,
>
> > >Hvordan tjekker jeg om et objekt er af en given
> > >type?
> > >F.eks. hvis "Geometry" er en klasse hvorfra jeg
> > >har nedarvet både "Circle" og "Square", så er
> > >begge self. af typen Geometry, men hvordan tjekker
> > >jeg om det er det ene eller andet?
> >
> > Det bør du ikke. Hvis du har noget funktionalitet der er forskelligt
> > afhængigt af type, bør det ligge i en virtuel metode i klassen.
> Hmm... Jamen for en cirkel ville det være rart
> hvis den f.eks. kunne aflevere sin radius, medens
> en firkant f.eks. kunne aflevere længden af
> diagonalen. Lidt søgte eksempler, men jeg mener
> ikke at alle nedarvede klasser kun skal kunne det
> som deres super klasse kunne/kræver.

Cirklens radius returneres af "Circle" ved at du udvider denne klasse
i forhold til base class "Geometry", og længden af diagonalen
returneres af "Square". "Geometry" tager sig kun af det, som er fælles
for alle geometriske objekter, f.eks. placering. Du kan også lave
virtuelle metoder, som hver afledt klasse så tvinges til at implementere,
f.eks. at tegne sig selv.

>
>
> > Men der findes altså en mekanisme for det, RTTI. Jeg har kun 2nd edition af
> > "C++ Programming Language" ved hånden, men den er nok nærmere beskrevet i 3rd
> > edition. Slå op under dynamic_cast, typeinfo, etc. Fx:
> >
> > class B ...;
> > class C : public B ... ;
> >
> > void foo(B *b) {
> >      if (dynamic_cast<C*>(b)) {
> >         // b er ikke bare en B*, men også en C*
> >      }
> > }
> >
> > Men: du bør ikke have behov for det i de fleste tilfælde, tænk lige over dit
> > design een gang til.
> Jeg tænker, jeg tænker... :o)
>
> Mvh,
>  Martin E. Nielsen

mvh
Jan



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:08 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *