Use C ++ to design a class that cannot be inherited
Source: Internet
Author: User
Question: use C ++ to design a class that cannot be inherited. Analysis: This is Adobe's latest exam for campus recruitment in 2007. In addition to the basic skills of C ++, this question is a good question. The keyword final is defined in Java, and the class modified by final cannot be inherited. However, if the keyword final is not found in C ++, it takes some effort to implement this requirement. In C ++, the constructor of the subclass automatically calls the constructor of the parent class. Similarly, the sub-class destructor automatically calls the parent class's destructor. To ensure that a class cannot be inherited, we only need to define its constructor and destructor as private functions. When a class tries to inherit from it, it will inevitably cause compilation errors because it tries to call constructor and destructor. However, the constructor and destructor of this class are both private functions. How can we get the instances of this class? We can create and release class instances by defining static resources. 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: finalcl Ass1 (){}~ Finalclass1 () {}}; this class cannot be inherited, but it is not convenient to use because it is different from the general class. For example, we can only get instances on the stack, but not instances on the stack. Can I implement a class with the same usage as a general class except that it cannot be inherited? There are always methods, but some skills are required. See 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 similar to a general class. You can create instances on stacks or stacks. Although the constructors and destructor of the class makefinal <finalclass2> are both private, because the class finalclass2 is its friend function, therefore, calling makefinal <finalclass2> constructor and destructor in finalclass2 will not cause compilation errors. However, when we try to inherit a class from finalclass2 and create its instance, It is compiled differently. Class try: Public finalclass2 {public: Try (){}~ Try () {}; try temp; because the finalclass2 class is inherited from the class makefinal <finalclass2> virtual, when calling the try constructor, the finalclass2 is skipped and the makefinal <finalclass2> constructor is called directly. Unfortunately, try is not a friend of makefinal <finalclass2>, so it cannot call its private constructor. Based on the above analysis, classes that attempt to inherit from finalclass2 will cause compilation errors once instantiated, so finalclass2 cannot be inherited. This satisfies our design requirements.
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.