On Thu, Nov 22, 2012 at 12:45 PM, Peter Kasting <
pkas...@chromium.org> wrote:
> On Thu, Nov 22, 2012 at 11:56 AM, Simon Hong <
simon....@gmail.com> wrote:
>>
>> For example, there are many child classes that inherits
>> WebContentsObserver.
>> There are two cases.
>> One is overriding methods as a public method.
>> The other is as a private method.
>>
>> Which is the right way?
>
>
> I'm not sure I understand. Are you asking what visibility child classes
> should give to overridden methods declared in a parent class? If so, the
> codebase is not consistent, and there are a couple of different views,
> though honestly the difference is not very large, so this is something of a
> bikeshed.
>
> The two main viewpoints are (1) use the same visibility as the parent class,
> and (2) make child methods "as private as possible".
>
> The advantage of (1) is that it's easy to implement and unsurprising --
> polymorphism does not change the "publicness" of a method accessed through a
> pointer.
>
> The advantage of (2) is that you can use it to enforce API correctness in
> certain cases. For example, you may have a class that inherits from
> NotificationObserver. It's usually wrong to call Observe() directly on such
> a class, as Observe() should normally only be called by the notification
> system. Making Observe() private in the class enforces this.
Not really. If you've publicly inherited from NotificationObserver
[1], then Observe() is still available to anyone who has access to
your Foo*. This is because they can simply upcast your Foo* to a
NotificationObserver* and call Observe() on that - perhaps violating
the expectations of your Foo*. This may not be done directly either
(eg: via a static_cast<NotificationObserver*>) - you may simply be
calling a method that expects a NotificationObserver* and the compiler
is doing the casting for you.
I would say that if you're trying to restrict the visibility of a
parent class's methods, you're doing the inheritance wrong, because
you're not really matching the same constraints and pre-/post-
conditions of the parent class. If the parent class says a method is
"Anybody can call this", then your derived class should also be
following the "Anybody can call this" design. If you're trying to
restrict the visibility of that, then in general, composition would
seem the more favourable approach here, based on the style guide.
My experience with bugs related to this pattern ("There's no way
anybody can call this method at the wrong time - it's private!") are
similar to my experiences with multiple inheritance [2] - they are
subtle and easy to miss/hard to spot, so if you find your design
gravitating towards making a base class's public method private in
your derived class, it may be suitable to do a design double-check and
make sure the abstraction is right.
>
> Personally I make a habit of doing (2) (since I'd rather always do one or
> the other, and I think the benefits of (2) are larger), but as I said, there
> is not broad agreement, and no style rule covers this.
>
> PK
>
> --
> Chromium Developers mailing list:
chromi...@chromium.org
> View archives, change email options, or unsubscribe:
>
http://groups.google.com/a/chromium.org/group/chromium-dev
[1]
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Inheritance#Inheritance
[2]
https://groups.google.com/a/chromium.org/d/topic/chromium-dev/mya3DMQkHWA/discussion