c++ - How to correctly check if an object implements an interface -


i want implement custom behavior objects.

for that, have items (inheriting qgraphicsitem) implement interface.

class someparentitem  {      someparentitem(bool x) { x = true; }      void function1() {} }; class someinterface {     virtual void function2() = 0; }; class xyzitem : public qgraphicsxyzitem, public someparentitem, public someinterface {     xyzitem(bool x):someparentitem(x) {}     virtual void function2() { x = false; } }; class mpqitem : public qgraphicsmpqitem, public someparentitem {     mpqitem (bool x):someparentitem(x) {} }; 

from outside, thinking

someinterface* item1 = dynamic_cast<someinterface*>(item); if(item1 == null)     item->function1();  else     item1->function2();   

unfortunately crashes... usually... creating flag test, , if flag true, dare cast.

but kept thinking, shouldn't crash. got brave , tried again, time in qwidget child. instead of crash got

qwidget::insertaction: attempt insert null action 

it test if(item1 == null) gives message...

how check correctly if item implements someinterface ?

note: item cannot null.

there may compiler switch disables run-time-type-information, in case explain behaviour. otherwise item1 should non-null, if dynamic_cast worked


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -