Implement a class that cannot be inherited in C + +

Source: Internet
Author: User

the thinking of a pen question: remember, when looking for a job, encountered such a pen question. Can't remember is that company's pen test, anyway feel oneself at that time also really took a bit of Kung Fu, but also took part in, the result is another part to despise! Now calm down the analysis is implemented as follows: Title: C++designing a class that cannot be inherited cannot be inherited? Cannot be inherited? Cannot be inherited? Based on the theoretical knowledge of inheritance, we can solve the problem by setting the constructor of the class to private. Because of that, the subclass has no way to access the constructor of the base class, thereby preventing the implementation of the task of constructing the object of the subclass and achieving the non-inheritable purpose. But, suppose so, how do we use this class in other places? The use of such a child has also created some obstacles. All right. Do you also think of defining a static method, implementing an object inside the method, and then returning its pointer. Ok? How do you release it? Then design a free memory function, the problem will be solved. Ok. Follow this logical analysis. The sample code is as follows: #include<iostream>using  namespacestd;classa{ Public:    StaticA * CONSTRUCT (intN) {A*PA =NewA; PA->num =N; cout<<"num is:"<<pa->num<<Endl; returnPA; }    Static  voidDestruct (A *pintance) {        Deletepintance; Pintance=NULL; }Private: A () {}~A () {} Public:    intnum;};voidMain () {A*f = A::construct (9); cout<<f->num<<Endl; A::D estruct (f);} Well, this class is like this. According to our theoretical analysis, the results of our practice are fully established. But, this question, it is more challenging, what does it mean? Do not you find that we are only qualified for the interview, but also can not be an exception to hire. What's wrong? You might ask me. Don't you really understand? Sure you don't see it? If it is true, then I will tell you! Our class cannot be implemented to create objects on the stack. That is, only one object can be built on the heap, and there is nothing on the stack. The great limitations of private constructors are so sweeping. All right! We modify it, so-called "patch Bar" for it, see the example code: #include<iostream>using namespacestd;template<typename t>classbase{friend T;Private: Base () {}~Base () {}};classFinalclass:Virtual  PublicBase<finalclass>{                 Public: Finalclass () {}~Finalclass () {}};voidMain () {Finalclass*p =NewFinalclass;//objects on the heapFinalclass FS;//object on Stack}ok. Now look at our Finalclass class. Inherits from Base,base as a virtual base class, because it is a friend of base, so it can access the private constructors of the base class, as well as the destructor. The compile run time is correct. That is, you can create objects on the heap, and you can build objects on the stack. Can it be inherited? If it is inherited by a class as a base class, it can be fully passed at compile time. There is no doubt that the problem is at runtime: when a subclass constructs an object, because it is a virtual inheritance, the constructor of the subclass directly calls the constructor of the base class, and the constructor of base is private. Run Error error!!! This is a class that really cannot be inherited.
Reprinted from: http://www.cnblogs.com/Braveliu/archive/2013/01/03/2842739.html

Implement 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.