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++?
{ Base::OnSize(hWnd, lParam); vScrollBar->setInfo(cyClient); }, assumingBaseis the base class.extend.forworks -- without missing the points where even those basic parts might be different from other languages, is an aquired skill. And it needs to be aquired, because one day you will be asked to work in an unfamilliar language, with very little time to prepare, and little leeway for trial & error.