Search code examples
c++privateprotectedderived-class

Use of protected specifier in a derived class


I am unsure of the correct access specifier for the member var isMouseOverYard. In the snippet I don't have plans to inherit from House. Option 1 is more consistent with the base class (if I were to inherit from either class I can check whether the mouse is over the object/yard). However, option 2 is more accurate of my current intention, if I do not inherit from House. Is there a convention in regards to this usage?

class Object
{
protected:
    virtual bool isMouseOverObject() const;
};

Option 1

class House : public Object
{
protected:
   virtual bool isMouseOverObject() const override;
   bool isMouseOverYard() const;
};

Option 2

class House : public Object
{
protected:
   virtual bool isMouseOverObject() const override;

private:
   bool isMouseOverYard() const;
}

Solution

  • My general rule of thumb is constraining the visibility to the biggest possible extent. I.e. I would make isMouseOverObject private in House class.