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.
You can actually do this, and it will work as expected. The only problem is that a
CMyWindowis still aCWindow, which allowsCreate():If you don’t want to allow this at any cost then you have to make
CWindow::Createabstract and possibly provide the current implementation (if any) as aprotectedmember that derived classes can call explicitly: