
On Thu, Jan 13, 2005 at 12:49:33PM +1000, kalin wrote:
Hi All, I've made a simple implementation of making a class non-derivable.
I can't compile this with GCC3 or Comeau. GCC2 lets me compile it (due to its flakey friend implementation) but it also allows Bar to inherit from Foo! This is a simpler way of preventing inheritance: class Finaliser { protected: Finaliser(){} }; class Final : private virtual Finaliser { // rest of class as normal }; I think I first saw this in Dr. Dobbs in one of their single page tips. It doesn't prevent derivation if the derived class has no user-defined constructors (since the compiler-generated ones will access the virtual base): class NotSoFinalNow : public Final { }; But does work if there are user-defined ctors: class WillNotCompile : public Final { WillNotCompile() {} }; // error There's another problem with Final that lets you derive from it in a class with user-defined constructors, if you know how. Our wiki where I work offers a chocolate bar to anyone who points it out but noone's claimed it from me yet :) jon -- "Consistency is the last refuge of the unimaginative." - Oscar Wilde