From: James Hopkin on
On Feb 23, 11:22 am, "Marco Nef" <maill...(a)shima.ch> wrote:
> >> Or is the virtuality inherited implicitly from the root of the
> >> inheritance tree even if it is omitted in intermediate classes?
>
> > Yes. C++ does not require the programmer to repeat the 'virtual' keyword
> > for a virtual method in a derived class.
>
>
> Some time ago I asked to change this in the new standard, so that virtual
> must be repeated in derived classes. But the reply in this group was that
> lots of old code would not compile anymore. That was a bad answer, as
> compilers could have a flag to ignore such an additional security feature in
> old code...
>

I think you have this the wrong way round. Changing the Standard to
make a huge amount of existing correct code invalid is a 'bad answer'.

A 'good answer', assuming for a moment that this requirement is a good
thing, would be for compilers to offer this feature as an extension.
In fact, Visual C++ does something similar, where you declare
overriding functions in derived classes both 'virtual' and
'override' (contextual keyword).

In my experience, errors of the kind you mention are common in code-
bases with deep and sprawling hierarchies. Component-based designs,
for example, suffer less, if at all. (A similar problem is that
specifying the correct base class to call is error prone - Visual C++
offers the __super keyword for this case).


James


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Chris Uzdavinis on
On Feb 23, 5:22 am, "Marco Nef" <maill...(a)shima.ch> wrote:
> >> Or is the virtuality inherited implicitly from the root of the
> >> inheritance tree even if it is omitted in intermediate classes?
>
> > Yes. C++ does not require the programmer to repeat the 'virtual' keyword
> > for a virtual method in a derived class.
>
> Which is one of the worst "features" of C++ in large projects, as it causes
> lots of errors (how do you know that a method is virtual if it is not
> written in the declaration of the class you are working with?). It should
> also be that base methods can only be called in the direct base class, or
> the programmer has to explicitly write a cast to skip a hierachy level.
>
> Some time ago I asked to change this in the new standard, so that virtual
> must be repeated in derived classes. But the reply in this group was that
> lots of old code would not compile anymore. That was a bad answer, as
> compilers could have a flag to ignore such an additional security feature in
> old code...

The new standard has something that will address this issue, using attributes
to designate that a function overrides (or hides) a base class name.
These attributes are [[override]] and [[hiding]], respectively. If your derived
class claims to override or hide, but doesn't actually do so, it is an error.

Further, you can opt-in to having a class require such attributes by adding
an attribute to the class, with the attribute [[base_check]]. Then, if a class
overrides or hides a base class name and doesn't indicate that it does, that too
is also a compile-time error.

See section 7.6.5 in the draft for details. Currently:
http://www.open-std.org/JTC1/sc22/wg21/docs/papers/2010/n3035.pdf

--
Chris


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Anders Dalvander on
On Feb 23, 8:57 pm, Edward Diener
<eldiener_no_spam_h...(a)tropicsoft.com> wrote:
> It is called documentation. That's how you know.

Sometimes the code is the documentation.

Anyway, templates can mess things up. See example below, and answer
the following question: Is Derived::DoIt a virtual member function?

template <class TBase>
class Derived : public TBase
{
public:
void DoIt()
{
std::cout << "Hello World" << std::endl;
}
};

If changing the rules to the suggestion of Marco, and others, the
answer is easy: No. (Otherwise a compile time error would occur.)
But according to the current rules the answer is: It depends.

Personally I haven't decided what I think is is best.

Regards,
Anders Dalvander


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]