Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Style: Prefixing virtual keyword to overridden methods

I've been having a discussion with my coworkers as to whether to prefix overridden methods with the virtual keyword, or only at the originating base class.

I tend to prefix all virtual methods (that is, methods involving a vtable lookup) with the virtual keyword. My rationale is threefold:

  1. Given that C++ lacks an override keyword, the presence of the virtual keyword at least notifies you that the method involves a lookup and could theoretically be overridden by further specializations, or could be called through a pointer to a higher base class.

  2. Consistently using this style means that, when you see a method (at least within our code) without the virtual keyword, you can initially assume that it is neither derived from a base nor specialized in subclass.

  3. If, through some error, the virtual were removed from IFoo, all children will still function (CFooSpecialization::DoBar would still override CFooBase::DoBar, rather than simply hiding it).

The argument against the practice, as I understood it, was, "But that method isn't virtual" (which I believe is invalid, and borne from a misunderstanding of virtuality), and "When I see the virtual keyword, I expect that means someone is deriving from it, and go searching for them."

The hypothetical classes may be spread across several files, and there are several specializations.

class IFoo { public:     virtual void DoBar() = 0;     void DoBaz(); };  class CFooBase : public IFoo { public:     virtual void DoBar(); // Default implementation     void DoZap(); };   class CFooSpecialization : public CFooBase { public:     virtual void DoBar(); // Specialized implementation }; 

Stylistically, would you remove the virtual keyword from the two derived classes? If so, why? What are Stack Overflow's thoughts here?

like image 365
erydo Avatar asked Sep 03 '09 01:09

erydo


People also ask

Can virtual methods be overridden?

A virtual method defined in the base class can be overridden in a derived class by defining a method with the same signature and marking it with the override keyword.

Where do I put virtual keyword?

The virtual keyword should be added to functions of a base class to make them overridable. In your example, struct A is the base class. virtual means nothing for using those functions in a derived class.

What does the virtual keyword do in C++?

A virtual keyword in C++ is used to create a virtual function in C++. The virtual function is the parent class function which we want to redefine in the child class. The virtual function is declared by using the keyword virtual.

What purpose does the keyword virtual serve?

A 'virtual' is a keyword preceding the normal declaration of a function. When the function is made virtual, C++ determines which function is to be invoked at the runtime based on the type of the object pointed by the base class pointer.


1 Answers

I completely agree with your rationale. It's a good reminder that the method will have dynamic dispatch semantics when called. The "that method isn't virtual" argument that you co-worker is using is completely bogus. He's mixed up the concepts of virtual and pure-virtual.

like image 154
Tyler McHenry Avatar answered Oct 07 '22 18:10

Tyler McHenry