Why does a virtual function get hidden?

Assuming you intended B to derive from A: f(int) and f() are different signatures, hence different functions. You can override a virtual function with a function that has a compatible signature, which means either an identical signature, or one in which the return type is “more specific” (this is covariance). Otherwise, your derived class function … Read more

C++ covariance in parameters

The return type is permissible since derived inherits from base, but the function parameter can’t work – not all base instances will be a derived also. What’s supposed to happen in the cases where func is called on a pointer to base with a parameter that’s not a derived? The most derived implementation isn’t callable.

C++ virtual function table memory cost

The v-table is per class and not per object. Each object contains just a pointer to its v-table. So the overhead per instance is sizeof(pointer) (usually 4 or 8 bytes). It doesn’t matter how many virtual functions you have for the sizeof the class object. Considering this, I think you shouldn’t worry too much about … Read more

C++ Virtual function being hidden

The issue that you are experiencing is related to how name lookup works in C++. In particular, when resolving a member, the compiler will look into the static type of the object on which the member is being accessed. If the identifier is found in that class, then lookup completes and (in the case of … Read more

Calling derived class function from base class

The code you’ve posted should work the way you want. Calling doSomething on an instance of derived will call the overridden start and stop functions defined in derived. There’s an exception to that, though. If you call doSomething in the constructor or destructor of base (whether directly or indirectly), then the versions of start and … Read more

Why are private virtual methods illegal in C#?

I note that there are two questions here. In the future you might consider posting two questions instead of combining two questions in one. When you combine questions like this often what happens is only the first one gets answered. The first question is “why are private virtual methods illegal in C#?” Here are the … Read more