C++/CLI being a .Net language it has its basic types directly mapping to that provided by the .Net framework. The structure below shows the various basic types of C++/CLI and the types they map to in the .Net framework.

C++/CLI
(use to declare)
.Net / CLI
Type
Declaration Description
bool Boolean bool isValid = true; true or false
char SByte char c = ‘a’; signed 8 bit integer
unsigned char Byte unsigned char c = ‘a’; 8 bit unsigned integer
wchar_t Char wchar_t wc = ‘a’ or L’a’ Unicode character
short Int16 short s = 123; 16bit signed integer
unsigned short UInt16 unsigned short s = 15; 16bit unsigned integer
int Int32 int i = -1000000; 32bit signed integer
long Int32 int i = -1000000; 32bit signed integer
unsigned int UInt32 unsigned int i = 50000; 32bit unsigned integer
usigned long UInt32 unsigned int i = 50000; 32bit unsigned integer
__int64 Int64 __int64 i64 = 2000; 64 bit signed integer
long long Int64 __int64 i64 = 2000; 64 bit signed integer
unsigned __int64 UInt64 unsigned __int64 i64 = 50000; 64bit unsigned integer
unsigned long long UInt64 unsigned __int64 i64 = 50000; 64bit unsigned integer
float Single float f = 1.04f; 4 byte single precision floating point
double Double double d = 1.0E-13 8 byte double precision floating point

Scope

In a class

Private

Only the local module can see the variable or function

Public

Global scope – all modules can see the variable or function

Protected

This is the same as private except for classes that inherit this class with will also be able to see the variable or function

Static

This is a special type of scope whereby every instance of the class will share the same value for this variable. E.G. if you have a class called humans you could have a static variable called eyes and on the first instance of that class set eyes = 2. Then for every other instance of the class you would not need to bother setting eyes as they will also = 2

Variable usage

Don’t use bytes for anything other than characters. It is considered normal practice to use int (a machine word) for all counting and arithmetic operations. The same goes for shorts. (Characters are signed on some implementations and unsigned on others).

int is typically a machine word with a minumim of 16 bits. However in practive almost all general purpose machines use 32 bits for ints. Short is typically half a machine word and long is either one or two machine words (on 32 bit machines ints and longs are usually the same size).

short, int and long are all signed by default. Specify unsigned for unsigned. unsigned on its own means unsigned int.

The C++ standard does not specify how signed types are represented at the bit level – each compiler decides how to do this. You can guarnatee that a signed byte will hold the values -*127 to 127, but many implementations will allow values from -1278 to 127. However you cannot guarantee that the left most bit is used by the compiler as the sign bit (although it typically is).

Strings

In C++ you have ‘char’ as in C. In C++.NET you also have ‘string’. You still have to define its length as for a char array, but this new type allows you to work with a string as a whole rather than with pointers to each character.

Floating Point

For floating point float/Single is typically not precise enough as its only guaranteed to offer 6 significant digits. The double type guarantees at least 10 significant digits which is sufficient for most calculations. It is almost always right to use a double as the processing hit its often negligeable.

Literal / Constant Values

To specify the size of a literal constant use the following:-

Size Suffix Usage
char ‘ ‘ ‘c’
wchar_t L L’c’ or L”abc”
short not available
int this is default
long L 123L
unsigned long UL 123UL
single F 3.14159F
double this is default
exponent of a floating point E 3.14159E0F
double long L 1.2345E1L
string “Hello World”
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *