2

I am new to C++, albeit with a small amount of programming experience, and wanted to know if there is any global consensus on including keywords in the definition or not.

For example (not a valid statement, but for example):

class Dog : Animal {
    void foo() const override final foo bar ... keywords;
}

Is it generally recommended (for clarity's sake) to do the same in the definition, or should I just leave them off?

Dog::foo() const override final foo bar ... keywords {
    //
}

Are there any keywords that MUST be included in the definition if included in the declaration?

  • 8
    Depends on the keyword. If you omit `const` you aren't defining the same function you declared. Not to mention others aren't allowed in an out of class definition. [Please choose a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – StoryTeller - Unslander Monica Aug 30 '17 at 10:08
  • Thank you! I didn't realise that. Is that the only main exception? Will take a look at the book thread, but found that they were either too beginner or too advanced for me with my experience. :( – Timothy Rune Aug 30 '17 at 10:10
  • It is necessary for the definition to include enough information (class name, function name, return type, argument types, `const` qualifiers, exception specification, etc) so the definition matches the declaration. Note that `final` is an identifier, not a keyword. Beyond that, your question is unclear. – Peter Aug 30 '17 at 10:11
  • @Peter - An identifier? Are you sure you did not mistype? – StoryTeller - Unslander Monica Aug 30 '17 at 10:13
  • @Peter Which, I guess, is where my question stems from: how much information in the definition is enough to match the function signature. – Timothy Rune Aug 30 '17 at 10:20
  • @TimothyRune - Rest assured, your compiler will tell you. There's no guesswork involved. – StoryTeller - Unslander Monica Aug 30 '17 at 10:25
  • @StoryTeller That makes sense. Thanks! :) – Timothy Rune Aug 30 '17 at 10:26
  • @StoryTeller - no, I did not mistype. – Peter Aug 30 '17 at 10:39
  • @Peter - Then you seem to be intent on confusing the OP for the sake on pedantry. This is a ["specifier"](https://timsong-cpp.github.io/cppwp/n4140/class.mem#nt:virt-specifier) when used in the context the OP is using it. – StoryTeller - Unslander Monica Aug 30 '17 at 10:44
  • @StoryTeller - Don't presume you can read or make accusations about my intent, simply because I respond to different aspects of a question than you do. – Peter Aug 30 '17 at 11:48
  • @Peter - It wasn't the aspect, but the content of the response to said aspect. Let's be accurate. Your intent is immaterial if the content doesn't help towards achieving it. – StoryTeller - Unslander Monica Aug 30 '17 at 12:31
  • Before you go getting on your high horse, StoryTeller, it might pay for YOU to check your comments for accuracy. Using the same source you linked to, have a look at ["lex.name"](https://timsong-cpp.github.io/cppwp/n4140/lex.name), particularly para 2 and table 3, which explicitly state that `override` and `final` are "identifiers with special meaning". – Peter Aug 30 '17 at 14:39
  • @Peter - Good thing I checked back. Or I would never have had a chance to answer. I never claimed it wasn't a valid identifier, I claimed your comment would confuse the OP. Because in this context it's **not** an identifier, since it doesn't identify any entity. – StoryTeller - Unslander Monica Aug 30 '17 at 16:08

1 Answers1

2

The answer depends on the specific keyword.

As StoryTeller notes in the comments, const is part of the function signature. A declaration with const and a declaration without declare two distinct functions (and overload resolution will determine which declaration will be used for in a given expression). The same applies to volatile. Hence, you'll must also use it in the definition, so that declaration and definition can be matched.

virtual must not be used in the member definition, and the same applies to override and final. These keywords do not affect the function signature.

So, there's no need for consensus, since it's not subjective. There are two hard rules, and one reason for those two rules.

Useless
  • 64,155
  • 6
  • 88
  • 132
MSalters
  • 173,980
  • 10
  • 155
  • 350