How to stop the class to be inherited by other class.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
The following code shows how to define a sealed class in C++/CLI.
Now B : cannot inherit from A as it has been declared as 'sealed'. Also a detailed explanation about sealed keyword can be found here http://msdn.microsoft.com/en-us/library/0w2w91tf.aspx
Update: Added C++/CLI , also other answers have shown the latest C++11 way of achieving the same using
final
keyword.There are two ways, the simple cheap, and the correct one. The two answers by @Naveen and @Nawaz deal with the correct one, that requires manual creation of a sealer class for each class that you actually want to seal.
The not fool-proof way, which is used in the adobe libraries is using a templated class for that. The problem is that you cannot declare the template argument as a friend, and that means that you will have to switch from
private
to the less safeprotected
:And you can automate it with a macro (I don't remember the exact flavor of the macro in Adobe's code):
Now this will catch people that mistakenly try to inherit without knowing that they shouldn't:
But it will not inhibit people that really want to from deriving, as they can gain access to the constructor by deriving from the template themselves:
I am not sure whether this will change in C++0x, I think I recall some discussions on whether a class template would be allowed to befriend one of it's arguments, but in a cursory search through the draft I cannot really tell. If that was allowed then this would be a fine generic solution:
You cannot. C++ is not Java or C#. And also there is no point, ever, IMHO.
Based on Bjarne Stroustrup's http://www.stroustrup.com/bs_faq2.html#no-derivation FAQ with small modification without friend keyword usage:
C++11 adds the ability to prevent inheriting from classes or simply preventing overriding methods in derived classes. This is done with the special identifier
final
. For example:or
Note that final is not a language keyword. It is technically an identifier; it only gains special meaning when used in those specific contexts. In any other location, it can be a valid identifier.
C++11 solution
In C++11, you can seal a class by using
final
keyword in the definition as:To know the other uses of final, see my answer here:
C++03 solution
Bjarne Stroustrup's code : Can I stop people deriving from my class?
Generic_lock
So we can make use of template to make the
Usable_lock
generic enough to seal any class: