Final class

Posted on 2004-11-18
Last Modified: 2010-04-01
How do I make a final class like in Java?
    LVL 10

    Accepted Solution

    There isn't any direct way in the language as such of doing it....but yeah, u could force it thru an indeirect method

    Make the constructor private as below

    class FinalClass {
         static FinalClass * Create_FinalClass();
         FinalClass(const FinalClass&);
         FinalClass& operator=(const FinalClass&);

    FinalClass* FinalClass::Create_FinalClass() {
         return new FinalClass();

    int main(int argc, char* argv[]) {
         FinalClass *f = FinalClass::Create_FinalClass();    
         delete f;
         return 0;

    LVL 30

    Expert Comment

    Just to add to above comment, you can use an auto_ptr so the class gets deleted automatically.

    #include <memory>

    void SomeFunct()
          std::auto_ptr<FinalClass> f(FinalClass::Create_FinalClass());

    Author Comment

    Thanks sys_Prog
    LVL 10

    Expert Comment

    U r welcome

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now