C++ equivilent of Directcast

inighthawki

Newcomer
Joined
Nov 29, 2005
Messages
14
in vb.net, if i wanted to convert types, i could use directcast and return a value from say an object:

Dim AL As ArrayList
AL = DirectCast(BinaryFormatter.Deserialize(FileStream), ArrayList)

and it could convert it to an arraylist, im trying to do the same in C++, but apparently C++ doesnt have the directcast function, and its not under the convert functions (::System::Convert::???)
any help appreciated, thx in advance
 
Have you tried the C# style cast syntax?

Code:
AL = (ArrayList) BinaryFormatter.Deserialize(FileStream);

That should work as C#'s syntax is taken from the C / C++ way of doing things.
 
i'll try later when i get the chance, thx for thr response, u guys alwasy seem to answer so quickly here reguardless the fact that nobody posts :P
 
I think a dynamic cast would be the closest to a DirectCast. C-style casts are considered bad practice by many--it is very flexible, but as a result sometimes what exactly it will do is hard to predict. A dynamic cast is the only non-C-style cast that will perform type checking (like any .Net cast).

Code:
someType someVarialbe = dynamic_cast<someType> (anotherVariable);
 
thx for the response, though plausiblydamp's idea did work, and as far as im concerned is fine for right now lol...
 
safe_cast is yet another alternative:

Code:
ArrayList ^AL = nullptr;
AL = safe_cast<ArrayList^>(BinaryFormatter::Deserialize(FileStream));
 
Last edited:
From what I've read after posting, dynamic_cast is closest to VB's TryCast (C# "as" keyword) and safe_cast is closest to VB's DirectCast.
 
Back
Top