1

It is very easy that we can make sure derived class must implement interface defined in base class.

That is pure virtual function.

For example:

class BaseClass
{
...
virtual void print()=0;
...
}

class DerivedClass :public BaseClass
{
// function must be implement, otherwise compiler will complain ...
void print()
{
}
};

Can we defined a static interface in base class and make sure the interface must be implement in derivate class?

I want something like this

class BaseClass
{
...
static void print(); // base class only define static interface
...
}

class DerivedClass :public BaseClass
{
// derived class must implement interface, otherwise compiler will complain ...
static void print()
{
}
};

I have no idea about this.

Thanks for your time.

Xu Hui
  • 1,213
  • 1
  • 11
  • 24
  • 1
    short answer is not natively, but you can document and, as there is no runtime polymorphism here, any missing print should lead to compilation problem. – OznOg Nov 10 '19 at 12:36
  • 1
    Possibly a dup of: https://stackoverflow.com/questions/19062733/what-is-the-motivation-behind-static-polymorphism-in-c – Eljay Nov 10 '19 at 12:38

1 Answers1

1

It is not possible to make a virtual static function. For the simple reason that when calling a static function, you always know the class that defines that function in compile time. Unlike virtual functions, where you don't know the type of the object whose method you're calling. For example:

class A
{
    public:
    virtual void f() {printf("A");}
};

class B : public A
{
    virtual void f() override {printf("B");}
};

void g(A& a)
{
    a.f();
}

int main()
{
    B b;
    g(b);
    return 0;
}

In the above example, inside the function g, the correct function is invoked (B::f). Even though while compiling the function it is not known what the type of its argument is (it could be A or any class derived from A). Without making f() virtual, you would have overloaded the method f, rather than overridden it. Which means that in the following example, the output would be "A", even though you might expect it to be "B":

class A
{
    public:
    void f() {printf("A");}
};

class B : public A
{
    void f() {printf("B");}
};

void g(A& a)
{
    a.f();
}

int main()
{
    B b;
    g(b);
    return 0;
}

This may cause serious bugs, and it is suggested to never overload base class methods, and to always use the override keyword when overriding a virtual method to escape those bugs.

When making a static function, you can simply overload it, it would not create a compilation error. However, you probably never should overload it, because it may hide a bug that is very difficult to track (you are certain that B::f() is being called while actually A::f() is being called). Furthermore, it is not possible to 'force' the derived class to implement a static interface, because there is no such thing as a static interface. Because you have no virtual static functions, you may not pass a reference or pointer to the interface that would implement this function.

iliar
  • 932
  • 6
  • 11