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

  • SEARCH
  • Home
  • 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 1112813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:45:57+00:00 2026-05-17T02:45:57+00:00

I was wondering if there is a way to declare an object in c++

  • 0

I was wondering if there is a way to declare an object in c++ to prevent it from being subclassed. Is there an equivalent to declaring a final object in Java?

  • 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-17T02:45:58+00:00Added an answer on May 17, 2026 at 2:45 am

    From C++ FAQ, section on inheritance

    This is known as making the class
    “final” or “a leaf.” There are three
    ways to do it: an easy technical
    approach, an even easier non-technical
    approach, and a slightly trickier
    technical approach.

    The (easy) technical approach is to
    make the class’s constructors private
    and to use the Named Constructor Idiom
    to create the objects. No one can
    create objects of a derived class
    since the base class’s constructor
    will be inaccessible. The “named
    constructors” themselves could return
    by pointer if you want your objects
    allocated by new
    or they could return
    by value if you want the objects
    created on the stack
    .

    The (even easier) non-technical
    approach
    is to put a big fat ugly
    comment next to the class definition.
    The comment could say, for example, //
    We'll fire you if you inherit from
    this class
    or even just /*final*/
    class Whatever {...};
    . Some
    programmers balk at this because it is
    enforced by people rather than by
    technology, but don’t knock it on face
    value: it is quite effective in
    practice.

    A slightly trickier technical approach
    is to exploit virtual inheritance.
    Since the most derived class’s ctor
    needs to directly call the virtual
    base class’s ctor
    , the following
    guarantees that no concrete class can
    inherit from class Fred:

     class Fred;
    
     class FredBase {
     private:
       friend class Fred;
       FredBase() { }
     };
    
     class Fred : private virtual FredBase {
     public:
       ...
     }; 
    

    Class Fred can access FredBase‘s ctor,
    since Fred is a friend of FredBase,
    but no class derived from Fred can
    access FredBase’s ctor, and therefore
    no one can create a concrete class
    derived from Fred.

    If you are in extremely
    space-constrained environments (such
    as an embedded system or a handheld
    with limited memory, etc.), you should
    be aware that the above technique
    might add a word of memory to
    sizeof(Fred). That’s because most
    compilers implement virtual
    inheritance by adding a pointer in
    objects of the derived class. This is
    compiler specific; your mileage may
    vary.

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

Sidebar

Related Questions

Wondering if there is any way to get the lambda expressions that result from
I'm wondering if there is a way to declare routes in MVC3 so that
I was wondering if there is a way to declare a method (in an
I am wondering if there is any way to declare a byte variable in
Just wondering is there any way I can check whether the url links to
I was wondering is there a way to give the user a small guided
I was wondering is there a way in visual studio 2010 to search in
I was wondering is there a way in PHP that you could tell where
I am wondering is there a way to render a partial view in the
I am wondering is there any way we can test the font size/color of

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.