Development: wxTNG/API

From WxWiki
Jump to: navigation, search

(RN)

Vadim mentioned on the list he was imagining an API without virtual functions.... here's what I sort of think he is thinking of...


#include <iostream>

namespace wx 
{
    template <class T> class WindowBase
    {
    public:
        bool Move(int x, int y)  //Call implementation method
        {
            return static_cast<T*>(this)->DoMove(x,y);
        }
    protected:
        bool DoMove(int x, int y); //native base implementation 
    };
    
    class Window: public WindowBase<Window>
    {
        //Nothing overridden as it uses base implementation
    };
    
    class Control : public WindowBase<Control>
    {
    public:
        //Override
        bool DoMove(int x, int y) 
        { 
            std::cout << "Control::DoMove(" << x << "," << y << ")" << std::endl;  
            return WindowBase<Control>::DoMove(x,y); 
        }
    };

    template <class T> bool WindowBase<T>::DoMove(int x, int y)
    {
        std::cout << "WindowBase::DoMove(" << x << "," << y << ")" << std::endl;
        return true;
    }
}


int main(int argc, char* argv[])
{
    wx::Control w;
    w.Move(1,1);
    return 0;
}

Well, that isn't 100% great example, but one gets the idea.

This works, but it does make the pointer-to-parent calling a derived member tricky...