c++inheritancevirtual-functions

Why does C++ not have Extend feature to virtual functions?


C++ allows defining default behavior of member functions using the virtual keyword, but what if I want to extend a member function to perform additional functionality? For example, I have the following code:

virtual void OnSize(HWND hWnd, LPARAM lParam) {
    cxClient = LOWORD(lParam);
    cyClient = HIWORD(lParam);

    rect = { 0, 0, cxClient, cyClient };
}

cxClient, cyClient, and rect are member variables that are updated on receiving a WM_SIZE message. If I want to to update the scrollbar in an inheriting class, I would have to override the function to add in this functionality but then I would need to repeat the code for updating the member variables:

void OnSize(HWND hWnd, LPARAM lParam) override {
    cxClient = LOWORD(lParam);
    cyClient = HIWORD(lParam);

    rect = { 0, 0, cxClient, cyClient };

    vScrollBar->setInfo(cyClient); // new functionality
}

Why does C++ not provide extend keyword to allow extending on the default behavior of a virtual function, that is, retaining the default behavior and incorporating additional functionalities. e.g:

void OnSize(HWND hWnd, LPARAM lParam) extend {
    vScrollBar->setInfo(cyClient); // new functionality
}

Is this considered in the upcoming revision of C++?


Solution

  • In DerivedClass::OnSize(), you can call BaseClass::OnSize() to achieve just what you want. No special keyword needed.

    struct BaseClass
    {
        virtual void OnSize(HWND hWnd, LPARAM lParam) {
            cxClient = LOWORD(lParam);
            cyClient = HIWORD(lParam);
    
            rect = { 0, 0, cxClient, cyClient };
        }
    
        // ...
    };
    
    struct DerivedClass : public BaseClass
    {
        void OnSize(HWND hWnd, LPARAM lParam) override {
            BaseClass::OnSize(hWnd, lParam);
            vScrollBar->setInfo(cyClient);
        }
    
        // ...
    };
    

    The C++ committee is hesistant to add additional keywords to the language, especially for things that have always been possible without them. There is the danger of rendering existing code, which might use macros, compiler extensions etc., invalid by such extensions of the language proper. Hence, I would be rather surprised to see extends being considered for upcoming revisions of C++. The existing construct allows derived classes to have code both before and after the call to the base class functionality, which is actually superior to extends.