delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/19/00:32:14

From: Adam Majer <ummajera AT cc DOT UManitoba DOT CA>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: C++ question...
Date: Thu, 18 May 2000 23:25:49 -0500
Organization: The University of Manitoba
Lines: 52
Message-ID: <Pine.GS4.4.02A.10005182318130.27359-100000@castor.cc.umanitoba.ca>
References: <20000519031143 DOT 3640 DOT qmail AT web108 DOT yahoomail DOT com>
NNTP-Posting-Host: castor.cc.umanitoba.ca
Mime-Version: 1.0
X-Trace: canopus.cc.umanitoba.ca 958710351 9580 130.179.16.20 (19 May 2000 04:25:51 GMT)
X-Complaints-To: Postmaster AT cc DOT umanitoba DOT ca
NNTP-Posting-Date: 19 May 2000 04:25:51 GMT
In-Reply-To: <20000519031143.3640.qmail@web108.yahoomail.com>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On Thu, 18 May 2000, Mohamed Saad wrote:

> Hello...
> i was just wondering...
> what is the difference between a reinterpret_cast and
> a dynamic_cast?
> what happens if i use a static_cast instead of a
> reinterpret_cast to cast a pointer to another
> pointer?? would any problems arise??

As I understand it, reinterpret_cast allows you to cast an int to a
pointer and stuff like that. It functions like a static_cast but is much
more forgiving. dynamic_cast casts one variable to another at run time. As
for example if you are passing a pointer to an abstract class and then you
want to make it into one of the derived classes. You have to use
dynamic_cast to make sure that you are using valid pointers otherwise it
goes boom and doesn't work..

Example (makes things a little more clear!)

class One{
	virtual void Nothing()=0;
};

class Two{
	virtual void Nothing(){cout <<"4";};
};

class Three{
	virtual void nothing(){};
}

And then you have a function,

void Something(One *p)
{
	Two *MyPointer = dynamic_cast<Two*>(p);
}

If you pass Three to Something (legal) the cast fails and you get NULL in
MyPointer. If you just cast it with any other cast, you would get compile
errors or code going heywire...

That's my explanation :)

reinterpret_cast would work and produce no errors but would introduce
bugs. static_cast would fail at compile time.

    Sincerely,
    Adam


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019