开发者

Do virtual functions have to be public?

Here is the way I have my base class working:

class AguiWidgetBase
{
    //variables
    AguiDockingEnum dockingStyle;
    std::string text;
    AguiRectangle clientRectangle;
    AguiColor tintColor;
    AguiColor fontColor;
    std::map<int,int,CmpIntInt> children;

    //private methods
    void zeroMemory();
    virtual void onPaint();
    virtual void onAddChildControl(AguiWidgetBase *control);
    virtual void onTintColorChanged(AguiColor color);
    virtual void onDockingStyleChanged(AguiDockingEnum style);
    virtual void onTextChanged(std::string text);
    virtual void onThemeChanged(const AguiTheme &theme);
    void (*onPaintCallback)(AguiRectangle clientRect);
    void (*onTintColorChangedCallback)();
    void (*onDockingStyleChangedCallback)();
    void (*onTextChangedCallback)();
    void (*onThemeChangedCallback)();

protected:
    AguiWidgetBase *parentWidget;
public:
    AguiWidgetBase(void);
    ~AguiWidgetBase(void);

    void addChildControl(AguiWidgetBase *control);
    void removeChildControl(AguiWidgetBase *control);
    AguiWidgetBase* getParent();
    void paint();
    void setTintColor(AguiColor color);
    AguiColor getTintColor();
    void setDockingStyle(AguiDockingEnum style);
    AguiDockingEnum getDockingStyle();
    void setText(std::string text);
    std::string getText();
    void SetTheme( const AguiTheme &theme);
};

Some of them work like this. There is a regular non-overridable funcion which calls the virtual function and the function pointer if its not NULL.

Will my virtual functions be able to once again go into the private scope wh开发者_JS百科en I create derived classes or must they be public?

I want to avoid them being public due to my design. Thanks


Virtual functions can have public, protected, or private access.

A discussion of them via the C++ FAQ.

  • Should I use protected virtuals instead of public virtuals?
  • When should someone use private virtuals?


They can be private and do not need to be public.


Though they can be public, it is not considered as good design principle as Herb Sutter says.


virtual functions can be private. This is because private means that the function cannot be called by derived classes. It does not prevent the entry to the v-table being overwritten. This means that the both the base class and the derived class will have access to the overwritten virtual function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜