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

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

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

PHP Tips-Getting the nodes list of xml doument with responseXml in ajax ,call image save in database,time difference etc

Getting the nodes list of xml doument with responseXml in ajax var obj = ""; function callAjaxObj() { try { obj = new XMLHttpRequest(); } catch(e) { try { obj = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { obj = ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { alert("your browser doesn't support ajax"); return false; } } } } function testResponseXml() { callAjaxObj(); obj.open("get","sample.xml",true); obj.onreadystatechange=function() { if(obj.readyState==4) { var doc = obj.responseXML.documentElement; //var doc = obj.responseXML; alert(doc.getElementsByTagName('user').length); } } obj.send(null); } Example of calender script in PHP calender script in PHP echo " $title $year "; echo "SMTWTFS"; $day_count = 1; echo ""; while ( ...