Skip to main content

Posts

Showing posts with the label dynamic caset pointer

Dynamic cast using c++

Dynamic cast using c++ dynamic_cast() is more useful to find out what kind of object is pointed to by the given pointer. This is used when you write a function that could accept any objects of the given class's hierarchy and then you take care of it inside the method. Like, function myMethod(Base *ptr) // can recognize any object that is of Base's family { if (dynamic_cast(ptr) != NULL) // the pointed-to object is of Child1 { Child1 *myChild = ptr; // here you take care of it as it is of a Child1 object // .. rest of the code } else if (dynamic_cast(ptr) != NULL) // the pointed-to object is of Child2 { Child2 *myChild = ptr; // here you take care of it as it is of a Child2 object // .. rest of the code } else if (dynamic_cast(ptr) != NULL) // the pointed-to object is of Child3 { Child3 *myChild = ptr; // here you take care of it as it is of a Child3 object // .. rest of the code } else { // the object pointed-to by ptr is not Base's class family/...