What are the differences between overriding virtual functions and hiding non-virtual functions?

What is function hiding? … is a form of name hiding. A simple example: void foo(int); namespace X { void foo(); void bar() { foo(42); // will not find `::foo` // because `X::foo` hides it } } This also applies to the name lookup in a base class: class Base { public: void foo(int); }; … Read more

What is the first (int (*)(…))0 vtable entry in the output of g++ -fdump-class-hierarchy?

Those are the offset-to-top (needed for multiple inheritence) and typeinfo (RTTI) pointers. From the Itanium ABI (you are not using the Itanium compiler, but their description of this is really good): The offset to top holds the displacement to the top of the object from the location within the object of the virtual table pointer … Read more

C++ object size with virtual methods

This is all implementation defined. I’m using VC10 Beta2. The key to help understanding this stuff (the implementation of virtual functions), you need to know about a secret switch in the Visual Studio compiler, /d1reportSingleClassLayoutXXX. I’ll get to that in a second. The basic rule is the vtable needs to be located at offset 0 … Read more

Where do “pure virtual function call” crashes come from?

They can result if you try to make a virtual function call from a constructor or destructor. Since you can’t make a virtual function call from a constructor or destructor (the derived class object hasn’t been constructed or has already been destroyed), it calls the base class version, which in the case of a pure … Read more

Why are C# interface methods not declared abstract or virtual?

For the interface, the addition of the abstract, or even the public keywords would be redundant, so you omit them: interface MyInterface { void Method(); } In the CIL, the method is marked virtual and abstract. (Note that Java allows interface members to be declared public abstract). For the implementing class, there are some options: … Read more

What is the performance cost of having a virtual method in a C++ class?

I ran some timings on a 3ghz in-order PowerPC processor. On that architecture, a virtual function call costs 7 nanoseconds longer than a direct (non-virtual) function call. So, not really worth worrying about the cost unless the function is something like a trivial Get()/Set() accessor, in which anything other than inline is kind of wasteful. … Read more