{"id":3791,"date":"2014-03-30T05:38:12","date_gmt":"2014-03-30T05:38:12","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/virtual-or-type-casting-collection-of-common-programming-errors\/"},"modified":"2014-03-30T05:38:12","modified_gmt":"2014-03-30T05:38:12","slug":"virtual-or-type-casting-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/virtual-or-type-casting-collection-of-common-programming-errors\/","title":{"rendered":"virtual or type casting-Collection of common programming errors"},"content":{"rendered":"<p>I have a question. I am trying to create the code to update my game but I have gotten into a &#8220;dilemma&#8221;. I don&#8217;t want to use virtual and the only reason for it is EVERYBODY i talked to (in forums, chats, friends) say that virtuals make the code really slow, so I did a research and found out that the lookup it does from vtable can decrease the performance by almost half. So, I use it for tasks that doesn&#8217;t need to be updated every frame. Everything worked fine until I got to the update\/render functions. Then I started thinking to find a workaround. Got an idea but first I would like to ask people who have knowledge on that one before implementing it.<\/p>\n<p>My game engine is very event driven. I can send any kind of data using events in between subsystems (graphics, ui, scripting). So, I am thinking of sending an event &#8220;renderScene&#8221; every frame. It sounds great to me but there is a problem. The structure of the event handlers are not that great and I really don&#8217;t want to improve this right now because it does a really decent job and my goal is to finish my game instead of fixing up the engine and never finish it (happened to me, so don&#8217;t want to go back to it again).<\/p>\n<p>My event handler have a function that registers events to functions (i call the handlers). But the problem with that function is, I need to do function bindings and stuff to register member functions. So, I found a workaround it &#8211; I create a static function and call the member function from it. This is how exactly a static function look like:<\/p>\n<pre><code>void GraphicsSubsystem::renderScene(Subsystem * subsystem, Event * event) {\n   GraphicsSubsystem * graphics = static_cast(subsystem);\n   graphics-&gt;renderScene();\n}\n\nvoid ScriptingSubsystem::runLine(Subsystem * subsystem, Event * event) {\n   ScriptingSubsystem * scripting = static_cast(subsystem);\n   Event1 * e = static_cast(event);\n   scripting-&gt;runLine(e-&gt;getArg());\n}\n<\/code><\/pre>\n<p>There arguments are always the abstract subsystem class and the base event class. The <strong>runLine<\/strong> function I have no problem casting because I dont run a line of code on every frame. However, the <strong>renderScene<\/strong> function makes me a little bit uncomfortable.<\/p>\n<p>tl;dr So, here is my question. Is static casting an object on every frame faster than calling a virtual function on every frame?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a question. I am trying to create the code to update my game but I have gotten into a &#8220;dilemma&#8221;. I don&#8217;t want to use virtual and the only reason for it is EVERYBODY i talked to (in forums, chats, friends) say that virtuals make the code really slow, so I did a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3791","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3791"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3791\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}