Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using override keyword in cpp file

26 views
Skip to first unread message

helpseeker

unread,
Feb 24, 2017, 3:29:37 AM2/24/17
to
Hi,

I would like to know what is the general guidance/preference for using the override keyword in cpp files. I personally think that it is helpful to have the override keyword in the cpp files too so that it is easy to identify which methods are overridden and which are not. But I know its not needed to have the override keyword in cpp files. Is there a bad side to it though - to have override keyword in cpp file?

Thanks

Alf P. Steinbach

unread,
Feb 24, 2017, 5:11:30 AM2/24/17
to
Your decision to use `override` should not be influenced by the kind of
file – whether header or implementation file like cpp.

The kind of file plays a rôle for the compiler's assumption of what
programming language the code is written in, and it plays a rôle for
your assumption about how the file will be used, e.g. that a header file
will be included and an implementation file used as the main code file
for a translation unit.

The by convention different usage means that one adopts some different
conventions in header files versus implementation files, e.g. for
include guards, use of `inline`, definitions of namespace scope
variables. But this does not extend to use of `override` or not. There
is no way that the different usage of different file types is related to
what `override` is about.

`override` is a safeguard against believing one overrides a base class'
member function that isn't there or isn't virtual, and it's a safeguard
against maintenance changing the signature of an overridden function,
e.g. by adding a defaulted argument. So, it's effectively a part of the
static type checking, even though it doesn't influence the type.
Essentially it says to the compiler, “this is what I intend, namely an
override, please check that for me”, and clearly that has nothing to do
with file types whatsoever.

The cost is mainly that the syntax is awkward, with `override` at the
end of the function head. That can present a formatting problem. But
it's IMO a very small problem, and the safety is well worth it.


Cheers & hth.,

- Alf

Paavo Helde

unread,
Feb 24, 2017, 6:32:51 AM2/24/17
to
On 24.02.2017 10:29, helpseeker wrote:
> Hi,
>
> I would like to know what is the general guidance/preference for using the override keyword in cpp files. I personally think that it is helpful to have the override keyword in the cpp files too so that it is easy to identify which methods are overridden and which are not. But I know its not needed to have the override keyword in cpp files. Is there a bad side to it though - to have override keyword in cpp file?

What do you mean by 'in cpp file'? If you mean if you should repeat the
override keyword in a member function definition outside of the class
definition, then this is expressly forbidden by the current standard
(C++14):

8.4.1/1: "A virt-specifier-seq can be part of a function-definition only
if it is a member-declaration."

9.2/8: "A virt-specifier-seq shall appear only in the declaration of a
virtual member function."


helpseeker

unread,
Feb 24, 2017, 5:58:21 PM2/24/17
to
Yes that is what I meant. I am curious why the C++ 14 standard decided to forbid it. I find it useful to easily identify methods that are overridden prompts me to then look into the header file for more details or the base class if needed.
0 new messages