Can I exclude a base class member from the derived class?

Question!

Let's say I have a class called CWindow:

class CWindow
{
public:
    virtual bool Create();
};

In the derived class CMyWindow, I want to overload the Create(void) method to Create(int someParam), BUT, I do not want the user to be able to call the Create(void) method, only the Create(int someParam). Is this possible? Could I do:

class CMyWindow : public CWindow
{
private:
    bool Create();
public:
    virtual bool Create(int someParam);
};

Is this valid? Will it basically make the formerly public member of CWindow be a private member of CMyWindow?

I imagine if it IS possible to 'exclude' a member from a class, that this is closest to doing so, because AFAIK there is no magic youcantbeamember keyword in C++

My best guess is that no, you cannot do this. But I'm just hoping because I would like to avoid making a base class that has everything except for the Create() member, and deriving CWindow and CMyWindow from CWindowBase.



Answers

It seems like you publicly inherit from CWindow to inherited its implementation. This should not be your intent when using public inheritance. Public inheritance means inheritance of interface. But you don't want to inherit CWindows interface, you want to hide it!

Think about an alternative design like this

class Window
{
public:
    // EVERY Window can create the window using this method
    virtual bool Create();  
};


class MyWindow : public Window
{
public:
    // Initialize MyWindow with an additional parameter, 
    // that is used to create the window
    MyWindow(int param);

    // Create the window using the parameter
    virtual bool Create() override;
private:
    int param;
};

class View
{
public:
    void AddWindow(Window& window)
    {
        myWindows.Add(&window);
        window.Create();  // I can call this on EVERY Window instance
    }
};
By : hansmaad


This video can help you solving your question :)
By: admin