C + + implements a class that cannot be inherited __c++

Source: Internet
Author: User
There is a class in Java that cannot be inherited, and that is the final class. There is no final class in C + +.

C + + There are many ways to implement a class that cannot be inherited. The main idea is that subclasses cannot construct parts of the parent class, and there is no way for the class to instantiate the entire subclass. This limits the inheritance of subclasses. So we can make the constructor declaration of the parent class private, but so the parent class cannot be instantiated and continue thinking 、、、
We can take advantage of the character that a friend cannot be inherited!
First, assume that cparent cannot be inherited. Let Cparent be a friend and subclass of a class, cparent can be constructed, but the Cparent subclass Cchild cannot inherit that friend attribute, so it cannot be constructed. So we introduced a cfinalclassmixin.
We are so hopeful about the function of this class:
Any class that inherits from it cannot be instantiated, and the class itself does not want it to be instantiated.
Implementing a constructor and destructor are all private classes. At the same time, we declare our cparent as friends in this class. The code is as follows:
Class Cfinalclassmixin
{
Friend class Cparent;
Private
Cfinalclassmixin () {}
~cfinalclassmixin () {}
};
Our cparent code should read as follows:
Class Cparent
{
Public
Cparent () {}
~cparent () {}
};
This class (note, at this time it can still be inherited), now we need it cannot be inherited. So just change the code to
Class Cparent:public Cfinalclassmixin
{
Public
Cparent () {}
~cparent () {}
};
Just do it. Now inherit a subclass from Cparent try
Class Cchild:public cparent{};
Compile the code to try, found: no effect!!
Now think back to the reason we're doing this, which is the principle of the scheme, which is to have the parent class have access to the constructor of the Mixin class, but the subclass is inaccessible.
Now look at our code and find that the parent class is a friend of the Cfinalclassmixin class and can access its constructors. Because a friend cannot inherit, Cchild cannot access the Cfinalclassmixin constructor. So it should not be instantiated.
Cchild does not have access to the Cfinalclassmixin constructor, but it does not have to call it! I think that's the reason for the problem. Cchild is constructed of cfinalclassmixin by Cparent, so this friend is of no use to him!
Now the problem is found. To solve is very simple. Just let Cchild have to call the Cfinalclassmixin constructor.
Do you remember the virtual inheritance? One feature of virtual inheritance is that the constructor of the virtual base class is constructed by the final subclass! So it is possible to change cparent from Cfinalclassmixin inheritance to cfinalclassmixin virtual inheritance. The code is as follows:
Class Cparent:virtual Public Cfinalclassmixin
{
Public
Cparent () {}
Cparent () {}
};
Try it now, OK.
But maybe some people will be afraid of many inheritance! But we don't have to be so worried here! Why? Because our Cfinalclassmixin class is pure!pure!. That means it doesn't have a member variable at all! Then we don't have to worry about the biggest problem of multiple inheritance. The data redundancy generated by the diamond inheritance. and ambiguity.
Now there is a shortage! That is, we can't use this cfinalclassmixin class every time we add a friend statement to a class! It's not a big deal, but I think it's going to work out because I fully trust c++!
The solution is also very simple! That's the use of templates! The specific description is omitted, gives the code everyone will know when you see it.
Here is the complete code for my test program (where the cfinalclassmixin has been changed to a template)
   [CPP] View plain copy #include   "stdafx.h"    #include  <iostream>   using  namespace std;      template<class t>   //Application Templates    class cfinalclassmixin   {       friend T;           private:       cfinalclassmixin () {}       ~cfinalclassmixin () {}  };                  class cparent:virtual public cfinalclassmixin<cparent >  //Virtual inheritance    {       public:          cparent () {}         ~cparent () {}  };       class cchild:public cparent{}; //subclass inherits from parent class       Int main ( int Argc, char* argv[])    {       CParent a; //  Can construct        cchild b; //cannot construct        return  0;  }  

Now just add a cfinalclassmixin mixed class to the class that you do not want to inherit to do the parent class.
by restricting constructors, we achieve the goal of restricting inheritance. But there are some exceptions, such as classes that are all static functions. The classes themselves do not need to be constructed. So we don't have a way with it. A class that is all static functions implies that the design of the program itself may have to be weighed.
This is actually just a small example of the use of Mixin classes (mixed classes). There are many other uses, such as Uncopiale and so on. Don't say much. What I want to say is that you may be more disgusted with multiple inheritance. But too much negation is not worth it. It is still a matter of debate whether the use of multiple inheritance should be used properly. I think the key to using a method is to use people.

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.