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. ...

Song- Khamoshiyan Piano keyboard Chord,Notation and songs Lyrics

Song Aankhen Khuli Ho lyrics notation

Song : Aankhen Khuli Ho Movie: Mohabbatein Notes used : W=>Western - C D E F G- A- B-/ H=>Hindustani - S R G M P- D- N- ( Here for western, G=G-, A=A-, & B=B- ) ( For hindustani, P=P-, D=D-, & N=N- ) Song I : Aankhen Khuli...Ho Ya.. Ho Bandh W=> A.... C... B..C.. E.. E...... A... A.... H=> D... S... N..S.. G G....... D... D.... Deedaar Un Ka Ho.o.taa Hai.. W=> A...B....A....D.BAG....ADB... H=> D...N...D.....R.NDP...DRN... Kaise Kahoon Main O..Yaaraa W=> B..D.. D....E.... D.....C..C..C... H=> N..R.. R....G... R.....S..S..S..... Ye Pyaar Kaise Hota Hai W=> E...B.....DB...AG...B..AA H=> G...N....RN...DP...N...DD (Tururu ru ru, ru ru rururu ru......) W=> AA...GA...BCE..., B...DB..GA H=> DD...PD...NSG..., N..RN.. PD Song II: Aa.aj He Kisi..par Yaa.ro.on..., Marke De..Khe..gein Hum W=> E....FEDCBABC.D.. D D......., G A B C.... E.......D...D..... H=> G....MGRSNDNS.R. R R......., P D N S.....G........R...R.... Pyaar Ho...