I have a base class and a derived one and I want to change base functions while keeping them static as they should be passed to other functions as static.
How can I do that?
A virtual function cannot be global or static because, by definition, a virtual function is a member function of a base class and relies on a specific object to determine which implementation of the function is called.
The Virtual Static Idiom is a very simple technique used in C++ to make static methods virtual. This way, static methods can be overridden by subclasses. Actually, it just binds a static (class) method to an instance by explicitly passing the this pointer.
A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.
A static method (or static function) is a method defined as a member of an object but is accessible directly from an API object's constructor, rather than from an object instance created via the constructor.
The ATL framework gets around the limitation of no virtual statics by making the base class be a template, and then having derived classes pass their class type as a template parameter. The base class can then call derived class statics when needed, eg:
template< class DerivedType >
class Base
{
public:
static void DoSomething() { DerivedType::DoSomethingElse(); }
};
class Derived1 : public Base<Derived1>
{
public:
static void DoSomethingElse() { ... }
};
class Derived2 : public Base<Derived2>
{
public:
static void DoSomethingElse() { ... }
};
This is known as Curiously recurring template pattern, which can be used to implement static polymorphism.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With