DEV Community

Discussion on: Virtual functions in C++

Collapse
 
pauljlucas profile image
Paul J. Lucas • Edited

Due to these extra steps virtual functions come at an extra cost, both in size and in speed.

"Extra," but relative to what alternative? The typical alternative of storing a "type" data member and switching on that value is generally worse (since the switch has to be done in many places). It's a bit of a disservice to state that virtual functions come at an extra cost without also mentioning that the alternatives also have a cost. There's no free lunch.