From: Edward Diener on
Gregory Bok wrote:
> Say you have a class with a virtual method, and then in a derived
> class, the virtual method is declared without the "virtual" keyword.
>
> class A
> {
> public:
> virtual void method();
> };
>
> class B
> {
> public:
> void method();
> };
>
> Is this effectively an emulation of the "final" construct, e.g. this
> becomes a leaf class in the absence of other overridable methods?

No.

> Does this impact the ability to treat instances of B polymorphically?

No.

> 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.

> Is
> the behavior here implementation dependent?

No.


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

From: Francis Glassborow on
Gregory Bok wrote:
> Say you have a class with a virtual method, and then in a derived
> class, the virtual method is declared without the "virtual" keyword.
>
> class A
> {
> public:
> virtual void method();
> };
>
> class B
> {
> public:
> void method();
> };
>
> Is this effectively an emulation of the "final" construct, e.g. this
> becomes a leaf class in the absence of other overridable methods?
> Does this impact the ability to treat instances of B polymorphically?
> Or is the virtuality inherited implicitly from the root of the
> inheritance tree even if it is omitted in intermediate classes? Is
> the behavior here implementation dependent?
>

Once a signature has been declared as virtual all derived implementaions
are virtual. Indeed one problem for some programmers is that there is
currently no way to remove the virtual qualification once it has been
added by a base class.

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

From: Marco Nef on
>> 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...

- Marco


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

From: Paul Bibbings on
"Marco Nef" <maillist(a)shima.ch> writes:
> 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 would be happy even with the flip side of your suggestion - leaving
the standard as is with regard to the virtual specifier yet having a
compiler flag that could turn *on* a non-standard requirement that the
virtual specifier be given everywhere, as an extension.

Regards

Paul Bibbings

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

From: Paul Bibbings on
Francis Glassborow <francis.glassborow(a)btinternet.com> writes:
> Once a signature has been declared as virtual all derived implementaions
> are virtual. Indeed one problem for some programmers is that there is
> currently no way to remove the virtual qualification once it has been
> added by a base class.

I've always felt that this was a failing in the standard specification,
not requiring the virtual qualification to be present in derived
implementations. When faced with a large project code base it requires
traversing a potentially deep class hierarchy right to the top to
discover the virtual status of methods declared in a class that is being
immediately subclassed. Of course there are other reasons that require
the implementer of subclass to have full knowledge of a class hierarchy;
information about an inherited interface, for example. However, in this
instance, what seems odd is that the omission of the virtual specifier
in an immediate base, where a method is in fact virtual, results in
`partial information'. It might be supposed, carelessly, that if I have
some information about an interface element, then I have *everything* I
need to know about it.

Regards

Paul Bibbings

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