Jump to: navigation, search

CDT/C editor enhancements/Override annotations and warnings in classes

This is a problem page. Please treat it as a discussion page and feel free to insert your comments anywhere. My open questions are in bold--Tomasz Wesołowski 12:43, 6 May 2010 (UTC)

Description

As C++ does not require a 'virtual' keyword on methods which override a virtual method in base class. On top of that, there's no standard encouraged way to indicate that (like @Override in Java). Therefore, analysis of unknown code may be difficult - to actually determine whether a given method is virtual or not, the programmer would have to browse the definitions of all base classes for matching methods.

Ranking

Category: navigation

Importance: 4/5 --Tomasz Wesołowski 12:43, 6 May 2010 (UTC)

5/5 --Jens Elmenthaler

4/5 --Kirstin Weber 08:35, 10 June 2010 (UTC)

Please append your opinion here.

Solution

Annotations

Probably the optimal solution is the one used in JDT: to use Annotations to mark declarations of methods which override a method in a base class. A click on an annotation would link to the declaration of the overridden method.

Detection

To actually determine if a method named X actually overrides another method X in a parent class is not a trivial problem in C++, as it may seem. There are some factors involved [1]:

  • Argument list
  • Method constness
  • Return type covariance [2]
  • throw() declaration
  • anything more which I may have omitted?

Possible override mistake warning

As there are many factors involved, it may be easy to make a mistake here. The programmer should be warned about possible threats:

  • Incompatible argument list or method constness will compile silently, but create a method overload instead (without overriding the original method). This is usually not the programmer's intent, so the programmer should be warned about this situation. (can we agree that doing this on purpose shall be considered bad practice and avoided?)
  • Incompatible return type or throw() declaration will not compile, so the compiler will warn about it anyway.

Those warnings (possibly with fixes) can be implemented as Codan checkers.

'virtual' keyword omission warning

It is considered good practice to add 'virtual' to a method declaration which overrides a method in a base class. (can we agree here?) In this case, a Codan checker and quick fix which would suggest to add 'virtual' for clarity to a declaration of a method which is anyway virtual because of an override.