[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



On Sat, 17 Mar 2001 10:06:18 +0100, Martin E. Nielsen wrote:

>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.

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.

-- 
=======================================================================
<sslug@sslug>           Herlev, Denmark       Software Designer
<URL:http://www.andreasen.org/>       <*>         LASAT^WEicon Networks
=======================================================================


 
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] *