是否可以禁止在编译时从一个类派生? [英] Is it possible to forbid deriving from a class at compile time?
问题描述
我有一个值类,根据C ++编码标准,第32项中的描述。总之,这意味着它提供了价值语义,没有任何虚拟方法。
I have a value class according to the description in "C++ Coding Standards", Item 32. In short, that means it provides value semantics and does not have any virtual methods.
我不想让一个类从这个类派生。除了别的,一个原因是它有一个公共的非虚拟析构函数。但是一个基类应该有一个析构函数是public和virtual或protected和nonvirtual。
I don't want a class to derive from this class. Beside others, one reason is that it has a public nonvirtual destructor. But a base class should have a destructor that is public and virtual or protected and nonvirtual.
我不知道写一个值类的可能性,不可能从中得出。我想在编译时禁止它。有没有可能有任何已知的成语呢?如果不是,也许在即将到来的C ++ 0x有一些新的可能性?
I don't know a possibility to write the value class, such that it is not possible to derive from it. I want to forbid it at compile time. Is there perhaps any known idiom to do that? If not, perhaps there are some new possibilities in the upcoming C++0x? Or are there good reasons that there is no such possibility?
推荐答案
即使问题没有标记为C ++ 11,对于在这里的人应该提到,C ++ 11支持新的上下文标识符 final
。请参见 Wiki页
Even if the question is not marked for C++11, for people who get here it should be mentioned that C++11 supports new contextual identifier final
. See wiki page
这篇关于是否可以禁止在编译时从一个类派生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!