Saif Mulla


C++ & Web

A brief overview of C++ Type conversion and RTTI

Posted by on | Category: C++


Type casting or Type Conversion is a process of converting a value of one data type to another data type. In C++, type casting takes place implicitly and explicitly, implicit type casting is done automatically by the compiler while explicit type casting is undertaken by programmer. There have always been confusion around C++ explicit type casting, particularly when dealing with polymorphic pointers and references. In this post I am trying to explain uses of C++ specific type conversion functions when dealing with user defined classes.

Coercion

the action or practice of persuading someone to do something by using force or threats.

Implicit Type conversion (Coercion)

Briefly speaking implicit conversion is done automatically by the compiler, which includes type promotions or conversions. Type promotion is when compiler promotes a lower type value to a higher type, such as when assigning double dd(0.010f), float value will automatically become 0.010 as double type. Moreover double dd = 3 will convert int type to double as 3.0, this is done by compiler itself by converting one type to a required type.

Explicit Type conversion

This type conversion is undertaken by programmer, by doing this programmer is determined by the type conversions being done, in fact he or she takes complete responsibility for casting. Mainly explicit casting is done when dealing with polymorphic objects and classes with inheritance relationship.

C Style

Conventionally C styled casts are well known amongst programmers such that converting from int to long or long to int long b = 100000; int a = (int) b; basically these type conversions are used on fundamental data types correspondingly conversion is done at compile time, therefore they are fundamentally safe.

Basically conventional style casts are rudimentary, therefore pose limitations when dealing with class hierarchies and strong type checking. Fortunately C++ provides operators to perform strong type checking on pointers and references types for polymorphic and non polymorphic classes utilised for upcasting from derived->base or down-casting from base->derived. Unlike conventional casts, C++ casting operators provides strict type checking and restricted casting, it ensures that not any pointer or reference type could be converted to any pointer type it must be complete object.

Accordingly, I will be using two classes as basis for explaining C++ casting operators, class Shape is a base class defined with a virtual function area and class Other is derived from base and overrides virtual function area.

class Shape {
	int R_;
public:
	Shape(){
		std::cout << "Base::Shape() " << std::endl;
	}
	virtual void area(){
		std::cout << "base::area " << std::endl;
	}
};

class Other : public Shape {
public:
	Other(){
		// std::cout << "Other" << std::endl;
	}
	void area(){
		std::cout << "Other::area " << std::endl;
	}
};
Const_cast

const_cast<>() operator is intended for conversion of non-constant or non-volatile pointers to constant or volatile types and vice-versa. Ideally const_cast can be used when passing a non-constant pointer to a function accepting const types as arguments, further more while calling a non-constant function through a constant pointer. For instance, a constant instance of class Other will generate compilation error.

```
const Other* ot = new Other;
ot->area();
passing 'const Other' as 'this' argument of 'virtual void Other::area()' discards qualifiers [-fpermissive]
  ot->area();
	```

Therefore simply replacing ot->area() with const_cast<Other*>(ot)->area(); resolves the issue, essentially const_cast simply converts a constant pointer to non-constant type to invoke non-constant function, similarly this conversion technique can be used for volatile and non-volatile pointers.

Reinterpret_cast

reinterpret_cast<>() is unportable and unsafe casting mechanism of C++, mainly because it simply cast pointers from one type to another irrespective of any relationship they posses, it simply converts pointers without type checking. Basically it creates a binary copy from one type to another. Generally reinterpret_cast is not recommended for use in your code however it is still available in case there is a need for it. The most common pointer conversion use of reinterpret_cast is while converting from void* to known object type, such as converting a void* allocated memory to an array of type int. int* arr = reinterpret_cast<int*>(voidArr*);

Static_cast

static_cast<>() can be used against C style cast but notably it allows conversion only on pointer or reference types related with inheritance relationship or between polymorphic types. Typically upcasting and down-casting of pointer types is possible with static_cast although but no assurance of safe typecast due to lack of type checking occurring at runtime. Type checking occurs at compile time therefore if used at runtime, then it is programmer


About

Hello there, I'm Saif Mulla and I'm a C++ Developer emphasising in performance optimisation and parallel programming with CPUs and GPUs. Basically everything to achieve faster code. This blog is my attempts at learning and imporving programming techniques and vast ocean of C++ language!

Recent posts