0% found this document useful (0 votes)
15 views

Nptel Docs Week-7

Uploaded by

Rounak Polley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Nptel Docs Week-7

Uploaded by

Rounak Polley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

NPTEL DOCS WEEK-7 ONWARDS

WEEK-7
 TYPE CASTING – PART 1

1. What is Type Casting? It’s types.

2. double f (int i, int j) {return (double) i / j;} How compiler


generates this code? Concept of mixed mode conversion.

3. double result = i / d;

If any of the i or d is double and result is declared as double, the


result we get in double.

4. Conversions:

i. int to double (ok!)


ii. double to int (warning’=’ : conversion from ’double’
to ’int’: possible loss of data)
iii. double * to int (Must be explicit!)
iv. Integer address to double address (Must be explicit)
5.

- Almost all casting are non-permissible.


-Only possible casting is the changing the
reference/address of pointer.

-Though it will not produce any errors, but it will


produce garbage value. That’s why forced casting is
dangerous.
6. Casting on a hierarchy:

 Upcast – okay!
 Downcast – No! error!
 A* to void* - okay! But lose of data!
 Void* to A* - No! Error!

7.

- Just understand this…

8. Till now, we have done all the casting as of the rules of c.

In CPP, there are some clear cast semantics:

- Const_cast (To omit the const-ness from a constant obj)


- Static_cast
- Reinterpret_cast
- Dynamic_cast
9.

- For the last example, it will not give any error in c-style
casting…which is very dangerous.

9. More examples on const_cast.


10. Exception:

-Here we are trying to make a const function as non-const


function. The reason is that const_cast is only intended to
remove const or volatile qualifiers from pointers or references to
variables—not from function pointers.

11. Syntax of storing a function address using pointer.


 TYPE CASTING-PART 2(STATIC CAST &
REINTERPRET_CAST)

1. Built in types

-double* to long is only permissible in c-style but risky.

-double* to int is not permissible due to size mismatch.

-void* to int* using static_cast is ok!

2. Class hierarchy. (upcast / downcast)


3. Static cast pitfall:1
static_cast<Window>(*this).onResize();
- This converts the *this (special window object) to the
window object. But compiler will not lose the original object.
That’s why it creates a temporary object and calls the
method on the temporary one, so it doesn’t make any
effect. (slicing)

- Solution:

Window::onresize()

4. Static cast pitfall:2 (Unrelated class)

- a = static_cast<A>(b);
why this is an error in case of unrelated class?

-Because A is not a built-in type. We should construct A


object. We can’t convert b onj to A out of nothing.

- Solution:
-To provide a constructor in A class of B type.
5. Static cast pitfall:3 (Unrelated class)

 From b we will return a()


 From a we will return an int.

6. Reinterpret cast: Avoid using it as much as possible.

- Used to perform any kind of pointer conversion.


 TYPE CASTING-PART 3(Dynamic_Cast)

 dynamic_cast only works with pointers or references to


polymorphic types—i.e., classes that have at least one virtual
function (typically, a virtual destructor).
 Main use case: Down casting.

-A* to void* is okay!

-But void* to A* is invalid!


 Since pA is nullptr, dynamic_cast will return nullptr for pC,
even though pA and pC point to types that are not related
through inheritance. The output will indicate that the cast was
done, but since both pA and pC are nullptr, this special case
allows the conversion to be valid.

1. typeid operator:
 When typeid is used with polymorphic objects (i.e., objects of a class with at least one
virtual function), typeid will return the dynamic type of the object, meaning the type of the
actual object that the pointer or reference points to.

 If the object is not polymorphic, typeid will return the type of the expression, which
could be the base class.

-Exceptions: When we delete before accessing the object, pointer


is still there but the reference is deleted.

2. RTTI: Run-Time Type Information


 MULTIPLE INHERITANCE:

 MULTUPLE INHERITANCE:
1. constructors and destructors
2. Object lifetime (program)

3. Diamond problem:

-Here two person object is created for creating a single


instance of TA.

Solution: (Virtual Base class, default ctor for virtual


inheritance)

You might also like