|
-
September 9th, 2008, 11:24 PM
#1
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.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
September 9th, 2008, 11:44 PM
#2
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.
- Alon
-
September 10th, 2008, 12:42 AM
#3
Re: A 'final' C++ class
This is related to some private techniques.
-
September 10th, 2008, 02:59 AM
#4
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|