CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    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

  2. #2
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    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

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: A 'final' C++ class

    This is related to some private techniques.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: A 'final' C++ class

    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured