tags:

views:

59

answers:

1

I basically have something like this:

class CGlToolBase
{
public:
    CGlToolBase(void)
    {

    }
    virtual void OnMouseDown(CGlEngine &glEngine, WPARAM wParam, LPARAM lParam);
    virtual void OnMouseMove(CGlEngine &glEngine, WPARAM wParam, LPARAM lParam);
    virtual void OnMouseUp(CGlEngine &glEngine, WPARAM wParam, LPARAM lParam);
    virtual void OnKeyDown(CGlEngine &glEngine, WPARAM wParam, LPARAM lParam);
    virtual void OnLDoubleClick(CGlEngine &glEngine, WPARAM wParam, LPARAM lParam);
    ~CGlToolBase(void);
};

class CGlToolSelect : public CGlToolBase
{
   bool selected;
public:
    CGlToolSelect(void)
    {
     selected = false;
    }
    virtual void OnMouseDown(CGlEngine &glEngine, WPARAM wParam, LPARAM lParam);
    virtual void OnMouseMove(CGlEngine &glEngine, WPARAM wParam, LPARAM lParam);
    virtual void OnMouseUp(CGlEngine &glEngine, WPARAM wParam, LPARAM lParam);
    virtual void OnKeyDown(CGlEngine &glEngine, WPARAM wParam, LPARAM lParam);
    virtual void OnLDoubleClick(CGlEngine &glEngine, WPARAM wParam, LPARAM lParam);
    ~CGlToolSelect(void);
};

In my select tool I set selected to false. Is this the correct way to do it if I do something like this:

CGlToolBase *tool = new CGlToolSelect;

Thanks

+3  A: 

That's perfectly legal and acceptable.

However, some notes:

Your destructor should be virtual. If any class has virtual methods (abstract or not), you should have a public virtual (abstract?) destructor (so delete calls the children destructors) or a protected non-virtual destructor (which prevents the superclass from being deleted as a superclass (it must be deleted as a subclass, thus calling the appropriate destructor)).

You may want to use initializers to set selected in the constructor:

CGlToolSelect() :
    selected(false)
{
}
strager
I use `(void)` because I'm a rebel. (It's a stylistic issue and nothing more; to me, it looks better.)
GMan
@GMan, Alright; it's just something I learned to avoid, but I can't remember why right now.
strager
Its called *destructor*, not *deconstructor*. Regarding your virtual dtor guideline - please take a look at Sutters [Virtuality](http://www.gotw.ca/publications/mill18.htm) (or at least #4 in the summary).
Georg Fritzsche
...or you can use protected non-virtual inheritance.
Clark Gaebel
Since you are adding style advice, I would not write the base constructor (empty initialization list, empty body).
David Rodríguez - dribeas
@strager: In C++ there is no difference between `void foo()` and `void foo(void)`, in both cases it represents a function taking no arguments. You probably learned that in C, where `void foo(void)` means a function taking no arguments, while `void foo()` is a function taking a fixed unknown number of arguments. In the first case, calls like `foo(5)` will be flagged as an error by the compiler in the first case, while it will be silently accepted in the second case. But again, in C++ both mean exactly the same.
David Rodríguez - dribeas
@David Rodríguez - dribeas, I understand that about C, but I wasn't sure if there was a difference in C++. I checked, and it seems there isn't, so I removed that from my answer. @Georg Fritzsche and @Clark Gaebe, You're right; I have never needed a protected destructor, so I never considered this. I also have problems remembering "destructor" or "deconstructor"; sorry about that.
strager