[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] C++ klass typer.



On Wed, Feb 12, 2003 at 10:12:57AM +0100, Joachim Holst wrote:
> Hej !
> 
> Jag undrar om det är möjligt att i C++ ta reda på vad klass X är för typ ?
> 
> Det är som så att jag har följande:
> 
> class A {
> public:
>   void print(void) = 0;
> };
> 
> class B : public A {
> public:
>   void print(void) { cout << "Hello World" << endl; };
> }
> 
> class C : public A {
> public:
>   void print(void) { cout << "BSOD again he !!" << endl; };
> }
> 
> int main(void) {
>   A *ap = NULL;
>   ap = new B;
>   b->print();
>   delete ap;
>   ap = new C;
>   ap->printf();
>   delete ap;
>  return 0;
> }
> 
> Hade velat kunan ta reda på om ap är en klass C eller B.
Hvis din print funktion have været erklæret virtual, så burde det hele
virke. Så finder programmet automatisk ud af at kalde den rigtige
version af print, alt efter hvilket object den er erklæret fra...

Hvis du på et tidspunkt har behov for at finde ud af om ap peger på
enten en C kan du gøre det ved hjælp af dynamic_cast:

C *cp = dynamic_cast<C*>(a);
if (cp) {
	// pointeren pegede på en C klasse
}

-- 
/-----------------------------------------------------\
| Klaus S. Madsen      | "Failure is not an option... |
| ICQ: 45400164        |  It comes bundled with your  |
| www.hjernemadsen.org |  Microsoft products!"        |
\-----------------------------------------------------/


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 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] *