C + + Design class can not be inherited method instances to explain _c language

Source: Internet
Author: User

The first thought is that in C + +, the constructor of a subclass automatically invokes the constructor of the parent class. Similarly, the destructor of a subclass automatically invokes the destructor of the parent class. To think of a class that cannot be inherited, simply define its constructors and destructors as private functions. So when a class tries to inherit from it, it is bound to cause a compilation error because of an attempt to invoke a constructor, destructor.

But the constructors and destructors of this class are all private functions, so how do you get an instance of that class? You can create and release instances of a class by defining static. Based on this idea, you can write the following code:

Copy Code code as follows:

///////////////////////////////////////////////////////////////////////
Define a class which can ' t be derived from
///////////////////////////////////////////////////////////////////////
Class FinalClass1
{
Public:
Static finalclass1* getinstance ()
{
return to new FinalClass1;
}

static void Deleteinstance (Finalclass1* pinstance)
{
Delete pinstance;
pinstance = 0;
}

Private:
FinalClass1 () {}
~finalclass1 () {}
};

This class is not inherited, but in general feel it and the general class somewhat different, it is also a bit inconvenient to use. For example, only instances on the heap can be obtained, and instances on the stack are not found.

Can you implement a class that is the same as a generic class, except that it cannot be inherited? There is always a way, but some skills are needed. Please see the following code:

Copy Code code as follows:

///////////////////////////////////////////////////////////////////////
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 does not differ from the general class, and can be created on the stack or on the heap. Although the constructors and destructors for class makefinal<finalclass2> are private, because the class FinalClass2 is its friend function, calling makefinal< in FinalClass2 Finalclass2> constructors and destructors do not cause compilation errors.

But when an attempt is made to inherit a class from FinalClass2 and create an instance of it, it differs by compilation.

Copy Code code as follows:

Class Try:public FinalClass2
{
Public:
Try () {}
~try () {}
};

Try temp;

Because the class FinalClass2 is inherited from the class makefinal<finalclass2>, the try constructor is called directly by skipping the FinalClass2 and directly calling makefinal< The Finalclass2> constructor. Unfortunately, try is not a friend of the makefinal<finalclass2>, so it cannot call its private constructor.

Based on the analysis above, classes that attempt to inherit from FinalClass2, once instantiated, cause compilation errors and therefore FINALCLASS2 cannot be inherited. This satisfies the design requirements.

The final keyword is already in c++11: its role is to specify that a virtual function of a class cannot be overridden by the class's inheriting class (override), or that a class is specified as a class that cannot be inherited (final Class).

Copy Code code as follows:

struct A
{
virtual void foo () final;
};

struct B final:a
{
void Foo (); Error:foo cannot be overridden as it's final in A
};

struct C:B//error:b is final
{
};

Related Article

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.