Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4242402
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:26:14+00:00 2026-05-21T03:26:14+00:00

Going under the assumption that there is a legitimate reason for preventing derivation from

  • 0

Going under the assumption that there is a legitimate reason for preventing derivation from some class, Bjarne gives a solution here for the answer to “Can I stop people deriving from my class?”

However, I thought of:

class final {
protected:
  final() { }         // line 3
};

class B : private virtual final {
};

class D : public B {  // line 9
};

int main() {
  B b;
  D d;                // line 14
}

When trying to compile, one gets:

foo.cpp: In constructor ‘D::D()’:
foo.cpp:3: error: ‘final::final()’ is protected
foo.cpp:9: error: within this context
foo.cpp: In function ‘int main()’:
foo.cpp:14: note: synthesized method ‘D::D()’ first required here 

Does my solution work for all cases? IMHO, it’s better because the class final is generic and doesn’t require foreknowledge of the class to prevent derivation from.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:26:14+00:00Added an answer on May 21, 2026 at 3:26 am

    The solution is rather bad, it could be improved with CRTP (adobe does so) but it won’t be a complete solution. The problem with your code is that another programmer that does not want to break your contract (she is a good person) but does not know that she should not derive from your class might want to lock others from deriving from her own class:

    class YourSealedClass : private virtual final {};
    class HerSealedClass : public YourSealedClass, private virtual final {
    };
    

    Note that there is not even malign intention, and the contract is broken. The improvement with CRTP would be:

    template <typename T>
    class seal
    {
    protected:
       seal(){}
       ~seal(){}
    };
    class YourSealedClass : private virtual seal<YourSealedClass> {};
    

    This will catch the previous mistake, as her code would be:

    class HerSealedClass : public YourSealedClass, private virtual seal<HerSealedClass> {};
    

    And as she is not inheriting from seal<YourSealedClass> the compiler will catch it up.

    The problem with this approach is that it does not block some stubborn (or malign) programmer from writing:

    class MalignClass : public YourSealedClass, private virtual seal<YourSealedClass> {};
    

    To become a child of the sealing class, and as such gain access to your own class.

    In C++0x I believe (would have to recheck it) that they will raise the restriction that a template cannot befriend a template argument, and that will be a good step into a generic typesafe solution for a class sealer:

    template <typename T>
    class seal {
       friend class T; // illegal in C++03
       seal() {};
       ~seal() {};
    };
    class MySealedClass : private virtual seal<MySealedClass>
    {};
    

    The advantage of this approach (currently unavailable) is that because the constructor and destructors are private only friends can instantiate the type. Because you are using the CRTP to inherit from an specific instantiation passing your own type as argument, you are telling the compiler that only you can actually instantiate your base, and all pieces fall right into place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I've always been under the assumption that you can't pass arrays between activities
Can anyone explain whats going on 'under the hood' with textRanges in IE/javascript? What
What user context do SharePoint timer jobs run under? The farm account? I'm going
While going through university and from following the development of SO, I've heard a
Is there any explanation of the sequence of events that occurs in the creation,
Motivated by this answer I was wondering what's going on under the curtain if
Going back to my previous question on OCSP, does anybody know of reliable OCSP
Been going over my predecessor's code and see usage of the request scope frequently.
After going through the Appendix A, C# Coding Style Conventions of the great book
What's going on here? printf.sh: #! /bin/sh NAME=George W. Bush printf Hello, %s\n $NAME

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.