Is it actually necessary to use either virtual
or override
?
I'm aware that there are a lot of questions on this general topic, e.g.:
- C++ “virtual” keyword for functions in derived classes. Is it necessary?
- Is the 'override' keyword just a check for a overridden virtual method?
- virtual? override? or both? C++
From those, and others marked as duplicates (the answers to a lot of the "duplicates" have contained distinct information that was new at least to me), I've learned some things (and, I think, roughly why they're true): override without virtual will not compile. Virtual without override will compile, but if you've made a mistake and don't have your method signature correct, the compiler won't tell you.
But what's going on if I omit both? Example:
struct BaseClass {
int a_number() {
return 1;
}
};
struct DerivedClass : BaseClass {
int a_number() {
return 2;
}
};
This compiles, and a_number
returns the appropriate result whether I instantiate BaseClass
or DerivedClass
. So it behaves as though I have overridden the function. Is there some reason why the code above is wrong? Is it a backwards-compatibility issue, or something more?
My apologies if this is answered directly in one of the related questions here that I missed, and thanks.
EDIT: StackOverflow keeps pointing me to the C++ “virtual” keyword for functions in derived classes. Is it necessary? question, as Wyck did below. I do not think it addresses my question, for the reason I gave him/her in the comments, which, since they are transitory, I'll repeat here: "I'm specifically asking about virtual in superclass methods, while that post seems to be about virtual in subclasses and how it propagates. The accepted answer below answers my question, but I don't think your link does (it might answer the question for someone more experienced in C/C++, but I don't think it answers it for a novice coming from Python and Java, like me)."
The point: I think the questions are related, but not the same.
I've accepted selbie's answer, since it was the first full-fledged "Answer" that answered my question. Wyck's answer provided a lot of useful, more general information.