• Saves Valuable Time
  • Trusted Accuracy since 2004
  • 15-Day Money-Back Guarantee

C++ Equivalent to VB Casts

In C++, the closest equivalent to the standard VB casting operators (CType and the corresponding CInt, CStr, etc.) is static_cast. The main exception are casts from floating point types to integer types (e.g., CInt(myDouble)) - in these cases, you need a method to perform VB 'banker's rounding' (our VB to C++ Converter inserts a helper class for this).

The behavior of all of these methods may be subtly different from the VB casting operators though, so you should always test the behavior of your converted C++ code.

The VB TryCast operator is converted to the C++ dynamic_cast operator.

VB C++
x = CBool(y)
x = CInt(y)
x = CChar(y)
x = CStr(y)

x = CType(myObject, FooType)
x = DirectCast(y, FooType)
x = TryCast(myObject, FooType)
x = static_cast<bool>(y);
x = static_cast<int>(y);
x = static_cast<wchar_t>(y);
x = static_cast<std::wstring>(y);

x = static_cast<FooType*>(myObject);
x = static_cast<FooType*>(myObject);
x = dynamic_cast<FooType*>(myObject);

Use VB to C++ Converter to convert from VB to C++.     

Additional resource: VB.NET and C++ Equivalents

Copyright © 2004 – 2021 Tangible Software Solutions, Inc.