Skip to main content

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/hierarchy
// throw an error
}

Remember, in C++, a base class type pointer can recognize/works with its derived/child class object.
Considering the same, polymorphic rule, in above method/example, you can use the myMethod() to manipulate any of the derived objects of Base.

As noted earlier dynamic_cast<> allows you to (downcast) type-cast a class pointer safely to an object in one single family/hierarchy of classes. That is called safe downcasting.

In C++ a base class pointer can "recognize" or "identify" a derived object same family.

dynamic_cast<> "helps" you to identify what exactly a pointer pointed to an object of the same family, but "never said" a base pointer can access a member of derived object that is in fact not a part of base class.

For examle,

if (dynamic_cast(baseTypePtr) != NULL)
{
Derived1 *d1 = baseTypePtr;
//you can refer it via d1, as inside the content is actually a Derived1 type object
}

If you give:
Derived1 *dptr = new Dervied1();
Base *bptr = dynamic_cast(dptr);

result:
the bptr is set to null as dynamic cast returns a NULL as the object actually/currently pointed to by dptr is not of Base class type, in fact, it is of Derived1 type. So that you would not access a method which is not actualla a part of the pointed-to object.

In simple, dynamic cast helps you to "identify" the object actually a pointer points to so you would assign it to right type pointer and access right type methods.
It helps you downcast it safely not blindly.

if you give:
Base *bptr = (Base *) dptr; // c style casting, not safe at all, and possibly error

result:
run time error (with a possible compiler type warning)

Comments

Popular posts from this blog

Resolved : Power BI Report connection error during execution

Getting Below Power BI Report connection error during execution . Error: Something went wrong Unable to connect to the data source undefined. Please try again later or contact support. If you contact support, please provide these details. Underlying error code: -2147467259 Table: Business Sector. Underlying error message: AnalysisServices: A connection cannot be made. Ensure that the server is running. DM_ErrorDetailNameCode_UnderlyingHResult: -2147467259 Microsoft.Data.Mashup.ValueError.DataSourceKind: AnalysisServices Microsoft.Data.Mashup.ValueError.DataSourcePath: 10.10.10.60;T_CustomerMaster_ST Microsoft.Data.Mashup.ValueError.Reason: DataSource.Error Cluster URI: WABI-WEST-EUROPE-redirect.analysis.windows.net Activity ID: c72c4f12-8c27-475f-b576-a539dd81826a Request ID: dfb54166-c78f-4b40-779f-e8922a6687ad Time: 2019-09-26 10:03:29Z Solution: We found report connection not able to connect to SQL Analysis service so tried below option. Re

Song- Khamoshiyan Piano keyboard Chord,Notation and songs Lyrics

All songs notation and chords at one place

Song : O Saathi Re Film : Mukhathar Ka Sikkandhar Uses : C D D# E G A Note : The numbers at the end of the lines indicate line numbers. Pallavi: O saathi re, tere binaa bhi kya jina, tere binaa bhi kya jina A- C D D#....,D D C DD E...C..CA-...,D D C DD E...CC.......1 Play line 1 again phulon men khaliyon men sapnom ki galiyon men GGG...GAGE.. GGG G A G E.................................................2 tere bina kuchh kahin naa E A G E D C D D#.......................................................................3 tere binaa bhi kya jina, tere binaa bhi kya jina D D C DD E....C..CA-..., D D C DDE....CC.............................4 Charanam: har dhadkan men, pyaas hai teri, sanson men teri khushboo hai CCC C D C A-, CCC C D C A-, DDD DED CD EE.. CCCC......................5 is dharthi se, us ambar tak, meri nazar men tu hi tu hai CCC C D C A-, CCC C D C A-, DDD DED CD EE.. CCCC......................6 pyaar yeh tute naa GGG... GAG D#......E.........................