I don't quite get what Dynamic_Casts and Reinterpret_Casts means

I don't quite get what Dynamic_Casts and Reinterpret_Casts means

Post by Kevin Won » Wed, 19 Jul 2000 04:00:00



I don't quite get what Dynamic_Casts and Reinterpret_Casts means.
Can any one give me some info about those key words?
Thank You.
 
 
 

I don't quite get what Dynamic_Casts and Reinterpret_Casts means

Post by Branimir Maksimovi » Wed, 19 Jul 2000 04:00:00



Quote:> I don't quite get what Dynamic_Casts and Reinterpret_Casts means.
> Can any one give me some info about those key words?

dynamic_cast performs checking whenever you want to do cast from base to
derived type, or across multiple inheritance hierarchy.
Further, in order to do  so, dynamic cast requires that every type must have
at least one virtual function.
That means that dynamic_cast performs run time type checking.
If cast cannot succeed,  null pointer is returned, but if you try to perform
cast to reference then bad_cast exception is thrown.

reinterpret_cast is something different, and should be used only as last
resort. It can be used to "reinterpret" some variable as it is something
else, which means, off course, not something that should be done every
day. But, reinterpret_cast cannot cast away (or bring)constness to some
type, for that job you need const_cast.

Quote:> Thank You.

P.S. For more info, and examples, you should refer to your favorite book.

Greetings, Bane.

 
 
 

I don't quite get what Dynamic_Casts and Reinterpret_Casts means

Post by John Harriso » Wed, 19 Jul 2000 04:00:00



Quote:> I don't quite get what Dynamic_Casts and Reinterpret_Casts means.
> Can any one give me some info about those key words?
> Thank You.

Suppose you have this

int *p = something();

but you know that, for some reason, p is really pointing at a double, then
you can do

double* q = reinterpret_cast<double*>(p);
cout << *q << endl;

This cast always succeeds but of course if p wasn't pointing at a double
then you're in trouble. This isn't something you need to do often.

Now suppose that you have this

class Base
{
public:
    virtual ~Base();
    // more stuff

Quote:};

class Derived : public Base
{
public:
    void some_dervied_only_func();
    // more stuff

Quote:};

Base* p = something();

and you think that p is really pointing at a Derived. This situation is
different from above, you have two related types who have virtual functions.
Now you can do

Derived* q = dynamic_cast<Dervied*>(p);
if (q)
    q->some_derived_only_func();

Now this cast may not succeed. If p wasn't pointing at a Derived then q will
be NULL. If p was pointing at a Derived then q will now point to that
Derived object.

I've simplified this slightly by ignore the possiblity that there are
further classes derived from Derived and by ignoring multiple inheritance.

john

 
 
 

I don't quite get what Dynamic_Casts and Reinterpret_Casts means

Post by Makhn » Wed, 19 Jul 2000 04:00:00


Quote:>That means that dynamic_cast performs run time type checking.
>If cast cannot succeed,  null pointer is returned, but if you try to
>perform cast to reference then bad_cast exception is thrown.

So if I created an object on the stack, and got a reference to it, like this

MyObject obj;
MyObject *objp=&obj;

If I tried to cast objp to one of MyObject superclasses, I'd get an
exception thrown?

 
 
 

I don't quite get what Dynamic_Casts and Reinterpret_Casts means

Post by Branimir Maksimovi » Wed, 19 Jul 2000 04:00:00



Quote:> >That means that dynamic_cast performs run time type checking.
> >If cast cannot succeed,  null pointer is returned, but if you try to
> >perform cast to reference then bad_cast exception is thrown.

> So if I created an object on the stack, and got a reference to it, like
this

> MyObject obj;
> MyObject *objp=&obj;

> If I tried to cast objp to one of MyObject superclasses, I'd get an
> exception thrown?

I didn't mean that. For upcasting you don't need dynamic_cast.
This is what I refering:

class A{
public:
virtual ~A(){}

Quote:};

class B:public A{

Quote:};
class C:public A{
};

int main()
{
 B b;
 static_cast<const A&>(b); // compiler don't need run time check for this
 dynamic_cast<const C*>(&b); // returns null pointer
 dynamic_cast<const C&>(b); // this is where we get run time exception

Quote:}

Greetings, Bane.
 
 
 

1. i don't know what it mean

i use Domino COM and when create Domino.NotesDatabase
my program will return this error , what it mean and how
to solve it

Server Error in '/MailToCustomer' Application.
-----------------------------------------------------------
---------------------

COM object with CLSID {29131524-2EED-1069-BF5D-
00DD011186B7} is either not valid or not registered.
Description: An unhandled exception occurred during the
execution of the current web request. Please review the
stack trace for more information about the error and where
it originated in the code.

Exception Details:
System.Runtime.InteropServices.COMException: COM object
with CLSID {29131524-2EED-1069-BF5D-00DD011186B7} is
either not valid or not registered.

Source Error:

Line 407:                        
Line 408:                      Domino.NotesDatabase dnd;
Line 409:                          dnd = new
Domino.NotesDatabaseClass();
Line 410:                         dnd.Open();  
Line 411:

Source File:
c:\inetpub\wwwroot\webservice\mailtocustomer\mailtocustomer
..aspx.cs    Line: 409

Stack Trace:

[COMException (0x80040154): COM object with CLSID
{29131524-2EED-1069-BF5D-00DD011186B7} is either not valid
or not registered.]
   MailToCustomer.WebForm1.Button3_Click(Object sender,
EventArgs e) in
c:\inetpub\wwwroot\webservice\mailtocustomer\mailtocustomer
..aspx.cs:409
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEve
ntHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent
(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent
(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain()

-----------------------------------------------------------
---------------------
Version Information: Microsoft .NET Framework
Version:1.0.3705.0; ASP.NET Version:1.0.3705.0

thank you
.

2. Velo wanted

3. Don't read this if you don't know windows

4. Milkshape Animations

5. You don't pay for what you don't use

6. SIMPLE CONCEPT FOR EXTRA MONEY

7. I don't succeed in getting the TCP Window size

8. does 'top' mean 'bottom' if not MM_TEXT?

9. Method '~' of object '~' failed --- What does this mean???

10. What's the meaning of the last 'const'?