Design a class that cannot be inherited in C + +

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_69d9bff30100odlz.html

The keyword final is defined in Java, and the final decorated class cannot be inherited. But without the final keyword in C + +, it takes some effort to implement this requirement. The first thought is that in C + +, the constructor of the subclass automatically calls the constructor of the parent class. Similarly, destructors for subclasses are automatically called by the destructor of the parent class. To think of a class that cannot be inherited, we simply define its constructors and destructors as private functions. Then, when a class attempts to inherit from it, it will inevitably result in compilation errors due to attempts to invoke constructors and destructors. But the constructors and destructors for this class are private functions, how can we get an instance of that class? It's hard to fail us, we can create and release instances of classes by defining static. Based on this idea, we can write the following code://///////////////////////////////////////////////////////////////////////Define a class which can ' t be derived from///////////////////////////////////////////////////////////////////////class FinalClass1 {public : static finalclass1* getinstance ()       { return new FinalClass1;       } static void deleteinstance ( finalclass1* pinstance)       { Delete pinstance; pinstance = 0;       }private : FinalClass1 () {} ~FinalClass1 () {} };This class can not be inherited, but always feel that it is not the same as the general class, it is also a bit inconvenient to use. For example, we can only get instances that are located on the heap, but not the instances on the stack. Can you implement a class that is the same as the general class except that it cannot be inherited? There is always a way, but some skills are needed. Take a look at the following code://///////////////////////////////////////////////////////////////////////Define a class which can ' t be derived from///////////////////////////////////////////////////////////////////////Template <typename t> class makefinal { friend T; private : makefinal () {} ~makefinal () {} }; class FinalClass2: virtual public makefinal<finalclass2> {public : FinalClass2 () {} ~FinalClass2 () {} };This class is not distinguished from the general class, and can be created on the stack or on the heap. Although the classmakefinal <finalclass2> Constructors and destructors are private, but because the class FinalClass2 is its friend function, it is called in FinalClass2 makefinal <finalclass2> Constructors and destructors do not cause compilation errors. But when we try to get fromFinalClass2 inherits a class and creates an instance of it, but it is compiled differently. class Try: public FinalClass2 {public : Try () {} ~Try () {} };Try temp; Because the classmakefinal <finalclass2> Virtual inheritance, in the call try constructor, will skip finalclass2 and directly call <finalclass2> constructor. It is very regrettable that makefinal <finalclass2> Friend, and therefore cannot call its private constructor.

Based on the above analysis, a class that attempts to inherit from FinalClass2, once instantiated, causes a compilation error, so FinalClass2 cannot be inherited. This satisfies our design requirements.

Design a class that cannot be inherited in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.