Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why use virtual functions in derived classes? #590

Closed
robwhess opened this issue Jul 2, 2014 · 2 comments
Closed

Why use virtual functions in derived classes? #590

robwhess opened this issue Jul 2, 2014 · 2 comments

Comments

@robwhess
Copy link

robwhess commented Jul 2, 2014

This is part question, part issue. I don't really understand the design decision to make functions in derived classes virtual. For example, each layer class derived from Layer has all of its functions (which override the ones in Layer) declared as virtual as well. Why are things done this way? Is the polymorphism here used anywhere beyond the initially derived classes themselves? I think there's typically a (very) slight effect on performance with virtual functions, so it might make sense to use virtual functions only when we know the polymorphism is going to be used.

@longjon
Copy link
Contributor

longjon commented Jul 3, 2014

I have wondered about this myself, so I did a little searching.

Virtual methods are implicitly virtual in derived classes; there is no "additional" penalty from using the optional keyword virtual when overriding a method. The use of the keyword is a stylistic choice; following the Google C++ style guide, we include it so that all virtual methods are explicitly noted as such.

See, e.g.,
http://stackoverflow.com/questions/2963965/why-is-virtual-optional-for-overridden-methods-in-derived-classes
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Inheritance#Inheritance

@longjon longjon closed this as completed Jul 3, 2014
@robwhess
Copy link
Author

robwhess commented Jul 3, 2014

Oh, huh. I didn't know overridden virtual methods were implicitly virtual. Thanks @longjon. Everything make sense now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants