c++ - Do polymorphic C-style casts have any overhead? -


  • does casting pointer instance of dervived class instances base class have run-time overhead in c++, or resolved @ compile-time?

  • if have any, has computed in casting operation?

example:

class foo; class bar : public foo;  bar* y = new bar; foo* x = (foo*) y; 

(i know should use c++-style casts , answer same them)

yes, though it's negligible.

in case, c-style cast interpreted static_cast, may incur pointer adjustment.

struct base {};  struct derived: base { virtual ~derived(); } // note: introduced virtual method  int main() {   derived derived;   base* b = &derived;   derived* d = (derived*) b;    void* pb = b;   void* pd = d;    printf("%p %p", pb, pd);    return 0; } 

this overhead happens whenever base subobject not aligned @ same address derived object, happens when:

  • introducing first virtual method
  • using multi-inheritance

of course, pointer adjustment counted negligible, , compiler shall smart enough eliminate if it's unnecessary anyway (an adjustment of 0).

note: implementation dependent, not mandated standard


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -