[osg-submissions] Fix "hidden virtual" warnings introduced since 2.2.0

Mike Garrity Mike.Garrity at mathworks.com
Tue May 27 07:46:04 PDT 2008


> From: osg-submissions-bounces at lists.openscenegraph.org [mailto:osg-submissions-
> Subject: Re: [osg-submissions] Fix "hidden virtual" warnings introduced since 2.2.0
>
> Robert is right, the "hidden virtual" warnings I want to eliminate
> indicate neither a bug in the compiler nor in OSG, because in this
> particular case, either (a) the methods in question are never invoked
> via a pointer to the subclass, only via a pointer to the base class (eg,
> osg::NodeVisitor), in which case they are not hidden and the
> polymorphism works exactly as intended, or (b) the C++ argument
> overloading semantics result in the correct (intended) method of the
> subclass being called (eg, in the case of osg:NodeVisitor, the
> inheritance graph of osg::Node and its subclasses exactly matches all
> the defined osg::NodeVisitor::apply() base methods).
>
> In any case, warnings from OSG headers are still a problem for me, and
> probably will be for others in future, too, since warnings-as-errors is
> a common practice, and it's inadvisable to suppress this particular
> warning, since in the general case, an unintentionally hidden virtual
> method can indeed be a bug and a hard to spot one at that.
>
> So I am re-submitting my patch, this time replacing all the extra
> methods with "using" declarations (as Tim Moore suggested), one per
> subclass wherever an overloaded virtual is overridden.  This does not
> introduce any new code that could make OSG more fragile, since the
> additions are not "code" per se (they are declarations).  I hope that
> this patch will pass muster where my original failed.  My only concern
> is that "using" declarations might not be supported by all C++ compilers
> currently in use by OSG users.  However, the "using" declaration has
> been part of the OSI C++ standard since 1996.
>
> -- Andrew Bettison

This question comes up pretty regularly on comp.lang.c++.moderated. I
think that this post by James Kanze does a pretty good job of summarizing
the "why" of it:

http://groups.google.com/group/comp.lang.c++.moderated/msg/9935661afbc910c0

My take from following these discussions is that the using declaration
is the appropriate way to deal with it when implementing the visitor
pattern in C++. The hiding of overloads is a tricky corner of C++, and
the using declaration seems to do a good job of stating in the code
that you're doing it deliberately while still avoiding nasty surprises
for users who inadvertently use a pointer to the derived class.

        -MPG-




More information about the osg-submissions mailing list