Syntax help

hi...

Can anyone help me in briefing the below code...

const MemoryMsg* memMess = safe_cast<const MemoryMsg*>(message.get());

Thank You
safe_cast<> is not c++. use dynamic_cast<> instead
Thank you very much...... But i need some explanation on what will happen if the above line is executed.
From MSDN:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// safe_cast.cpp
// compile with: /clr
using namespace System;

interface class I1 {};
interface class I2 {};
interface class I3 {};

ref class X : public I1, public I2 {};

int main() {
   I1^ i1 = gcnew X;
   I2^ i2 = safe_cast<I2^>(i1);   // OK, I1 and I2 have common type: X
   // I2^ i3 = static_cast<I2^>(i1);   C2440 use safe_cast instead
   try {
      I3^ i4 = safe_cast<I3^>(i1);   // fail at runtime, no common type
   } 
   catch(InvalidCastException^) {
      Console::WriteLine("Caught expected exception");
   }
}
It works only with the microsoft compiler and the option: /clr

In other words: safe_cast<> (as well as dynamic_cast<>) checks the type of the object and (unlike dynamic_cast<>) it throws an exception if the cast isn't possible (dynamic_cast<> returns NULL in this case)
Topic archived. No new replies allowed.