48. Classes that cannot be inherited

Source: Internet
Author: User

Title: Design a class in C + + that cannot be inherited.


Class SealedClass1
{
Public
Static sealedclass1* getinstance ()
{
return new SealedClass1 ();
}
static void Deleteinstance (Sealedclass1* pinstance)
{
Delete pinstance;
}
Private
SealedClass1 () {}
~sealedclass1 () {}
};


Solution two: Using Virtual inheritance

Implement a type that is the same as a generic type, except that it cannot be inherited from other usages.

Template<typename T>class makesealed
{
Friend T;
Private
Makesealed () {}
~makesealed () {}
};
Class Sealedclass2:virtual public makesealed < SealedClass2 >
{
SealedClass2 () {}
~sealedclass2 () {}
};


Template parameter types are not supported as friend types in the new GCC.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

48. Classes that cannot be inherited

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.