-
A 'final' C++ class
Maybe my memory's playing tricks - but some time recently I'm sure I read an article somewhere about a technique for producing a 'final' C++ class (in other words, a class from which other classes can't be derived). Does this ring any bells with anyone? I can't remember where I saw it or how it was done but I'd like to find out.
-
Re: A 'final' C++ class
Yeah, it's doable. I can't seem to find the article but the technique involves virtually inheriting from a class whose constructor is private and which declares the "final" class as a friend. This works because the most-derived class is responsible for constructing virtual base class objects, and only the final class has access to that constructor.
Hopefully I've got this right, but it should be something like:
Code:
class Base
{
};
class FinalHack
{
private:
friend class Derived;
FinalHack() {}
};
class Derived : public Base, virtual FinalHack
{
};
Would I ever use this? No.
-
Re: A 'final' C++ class
This is related to some private techniques.
-
Re: A 'final' C++ class