相关文章推荐
开心的柳树  ·  Static Library ...·  1 年前    · 
踏实的铅笔  ·  python - Pandas ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Today I was pretty happy when I learned that C++11 now finally knowns the final keyword. With it you can easily define a whole class as final and even single virtual methods. But I wonder why this is not possible for non-virtual methods? Let's take this example:

class A {
public:
    void m1() { cout << "A::m1" << endl; };            
    virtual void m2() { cout << "A::m2" << endl; };      
class B : public A {
public:
    void m1() { cout << "B::m1" << endl; };
    virtual void m2() { cout << "B::m2" << endl; };

Here I can easily prevent B from overriding the virtual m2 by declaring A::m2 as final. I would like to do the same with A::m1 so B can't hide A:m1 with it's own implementation of the method. but the compiler doesn't accept the final keyword without virtual. And I wonder if there is a reason why C++11 doesn't allow this and if I misunderstood something completely. In my opinion it makes perfectly sense to define a non-virtual method as final because I didn't declare it as virtual because I don't want others to override/hide it anyway (Which I can now enforce with final but unfortunately only for virtual methods...)

I like class designs where everything except abstract methods are final. It looks like this means I have to declare all methods as virtual now to be able to do that. Is that a good idea or are there reasons against it? For older C++ versions I often read that is a bad idea to declare all methods as virtual. Or maybe there is a better way to prevent hiding non-virtual methods?

because non-virtual methods are final already. you can't override them, you can only hide them. – Bryan Chen Jul 3, 2014 at 5:41 @Bryan Yes, you`re right, but that's still something I want to prevent. I changed the question to make this clear. – kayahr Jul 3, 2014 at 5:49 There is no way to prevent function hiding in general. You can , in class B, put using declarations to inject any functions from A that you don't want to hide. – M.M Jul 3, 2014 at 5:52 If there is no technical reason to protect a method/class from overriding with final simply don't do it. After the final keyword comes up I saw a lot of code with final in libraries. And we must them all remove while using the class/method in an extended way. It feels a bit as written by a smart alec if we find final in the code. And often the author of the code can't know that the method is really the final one :-) Our coding standards contain a don't use flag for final. I personal think that final is often more wrong than a goto! – Klaus Jul 3, 2014 at 7:29

According to the C++11 standard, you are explicitly not allowed to do so for functions. The relevant passage is under § 9.2/8:

A virt-specifier-seq shall contain at most one of each virt-specifier. A virt-specifier-seq shall appear only in the declaration of a virtual member function (10.3).

A virt-specifier includes final and override.

My guess is that they thought that these specifiers didn't make sense to be used in non-virtual functions since non-virtual functions are final by default and they are the "final overrider" as the standard states in other sections.

It looks like this means I have to declare all methods as virtual now to be able to do that. Is that a good idea or are there reasons against it?

I recommend against it -- as having virtual functions has different effects on the code which you may not want. For example, the class will now have to keep a vtable and lose its POD status. Overall it seems like a bad move if you just want to use the final keyword.

A virtual final method that doesn't override has no reasonable reason to have a vtable; I expect most implementations will drop the ball and still have one. (Sadly, there isn't a new keyword that states this method does not override, otherwise virtual final new would be an amusing way to guarantee non-virtual behavior on a call to a method) – Yakk - Adam Nevraumont Nov 9, 2015 at 16:21

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.