tags:

views:

131

answers:

4

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 when I create derived classes or must they be public?

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

+2  A: 

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

sth
I'm pretty sure they can't be private, because then nothing can override them.
Daniel T.
@Daniel: Perhaps surprisingly they can be overridden even if they are private. (For more discussion of the details of virtual functions also see here: http://www.gotw.ca/publications/mill18.htm)
sth
@Daniel T.: You can still override a private virtual method in a derived class. What you can't do is call the base class version.
aschepler
Whoops, just noticed that this was tagged C++. I was referring to C#. Sorry about that.
Daniel T.
+8  A: 

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

A discussion of them via the C++ FAQ.

birryree
+1  A: 

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.

doron
+2  A: 

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

Chubsdad