Virtual table/dispatch table

It’s sometimes easier to understand with an example: class PureVirtual { public: virtual void methodA() = 0; virtual void methodB() = 0; }; class Base : public PureVirtual { public: virtual void methodA(); void methodC(); private: int x; }; class Derived : public Base { public: virtual void methodB(); private: int y; }; So, given … Read more

Practical usage of virtual functions in c#

So basically if in your ancestor class you want a certain behaviour for a method. If your descendent uses the same method but has a different implementation you can override it, If it has a virtual keyword. using System; class TestClass { public class Dimensions { public const double pi = Math.PI; protected double x, … 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

Difference between virtual and abstract methods [duplicate]

Virtual methods have an implementation and provide the derived classes with the option of overriding it. Abstract methods do not provide an implementation and force the derived classes to override the method. So, abstract methods have no actual code in them, and (non-abstract) subclasses HAVE TO override the method. Virtual methods can have code, which … Read more