Skip to content

Latest commit

 

History

History
49 lines (33 loc) · 2.15 KB

implicit_casting.md

File metadata and controls

49 lines (33 loc) · 2.15 KB

Implicit casting, also known as automatic type conversion or coercion, is an automatic type conversion by the compiler. It happens whenever an expression of one type is used in a context where another type is expected. This process doesn't require any explicit operator or function to initiate it. The compiler does it on its own to get the appropriate data type.

There are some rules and guidelines about when and how these conversions can occur. Generally, implicit conversions can happen when:

The types are compatible

For instance, if you have a floating-point number and an integer, the integer can be implicitly converted to a float.

float f = 3.14;
int i = 2;
float result = f + i;  // i is implicitly converted to float

In the above code, the integer i is automatically converted to a float when it is added to f, because the addition operation requires two operands of the same type.

Converting to a higher precision or larger data type (Widening Conversion)

When an integer is converted to a float, or a short is converted to an int. These conversions usually don't lead to any data loss. For example:

int i = 97;
double d = i;  // i is implicitly converted to double

When passing arguments to functions

void func(double d) { /* ... */ }

void main() {
    int i = 3;
    func(i);  // i is implicitly converted to double
}

Converting derived class objects to base class (upcasting)

It is always safe to assign an object of a class to a reference or a pointer of its base class. This is because a derived class object includes all data members of the base class, so the derived class is a superset of the base class.

class Base { /* ... */ };
class Derived : public Base { /* ... */ };

Derived d;
Base& b = d;  // d is implicitly upcasted to Base

Downcasting, on the other hand, is not always safe. Downcasting is when you convert a base class pointer or reference to a derived class. It's unsafe because the base object might not be a complete derived object. If you try to downcast to the wrong type, the behavior is undefined. In C++, dynamic_cast can be used for safe downcasting, as it includes a runtime check.