I have an abstract base class with a virtual method. In the derived class, this method is implemented. However, I want the function in the derived class as a static method in order to be able to call the function without instantiating an object of that class.
class Base
{
virtual double Foo(double rParam) const;
};
class Derived1 : public Base
{
static double Foo(double rParam);
};
class Derived2 : public Base
{
static double Foo(double rParam);
};
Essentially, Derived1 and Derived2 provide different implementations of a static function (that do not depend on object data), but I want those functions to be virtual in order to be able to call those functions in other functions of the base class. The only solution I see right now is to implement two member functions in the derived class, one being the virtual function from the base class, and the other one (with a different name) being a static function. In order to prevent doubling the source code, the virtual function could then directly call the static function (could be inlined). Any other solutions?
class Derived : public Base
{
double Foo(double rParam)const
{
return FooStatic(rParam);
}
inline static double FooStatic(double rParam);
};
Foo
is marked asvirtual
, it means it depends on the runtime type of the object, so it can't not be coupled with an instance of a class. – Luchian Grigore