12/11/2007

A sealed class in C++

I want a 'sealed' class in native C++ just like 'sealed' in C++/CLI and C# or 'final' in Java. I googled it then got following solution.

template<typename T>
class DeriveLock {
private:

DeriveLock() {}
~DeriveLock() {}
friend T;
};

class SealedClass : virtual public DeriveLock<SealedClass> {
};

You can found above sample code at Bjarne Stroustrup's article.

1 comment:

Anonymous said...

Well, template friends is a non-standard C++. To get it working you have to implement different workarounds for different compilers. Some of them will not support it at all, unfortunately...

I wrote some article and code on this issue too, check it out - http://www.jetsnail.com/tech-blog/38-cpp/52-noninheritable