Teaches you how to create a special class in C + +

Source: Internet
Author: User

As far as language is concerned, individuals prefer C + +, although some of the grammatical aspects of C + + are quite esoteric, but these do not block the place where it is irreplaceable in practical applications.

Start talking about today's focus, how to define a special C + + class.

1. Define C + + classes that cannot be inherited

How do you make a class incapable of being inherited? Simply put, the effect we want to achieve is that if you inherit this class, the compiler will directly error.

To implement this class, I want you to know the following simple syntax for C + + in advance: Friend class , virtual inheritance . Here I'll show you how to define it, and then we'll discuss why.

The first step: Define an empty Class A, explicit construction and destruction , constructs and destructors must be defined as private.

The second step: define the non-inherited class B, give the normal construction and destruction, the public type, let b virtual inherit a class , inherit the way not limited , at the same time, B is set to a friend class .

Step three: Define Class C to try virtual inheritance Class B, compile error!

The code is as follows:

Class A{friend Class B;private:a () {}~a () {}};class b:virtual public a{public:b () {}};class C:p ublic b{public:c () {}};


Let's discuss why the compilation will go wrong.

First, the constructors and destructors for Class A are defined as private, and if there is no display definition, the default constructs and destructors are public. Can that be defined as the protected type? The answer is "NO". If you understand inheritance, you should know that member variables or methods of the private type in the base class are not visible in all derived classes, but member variables or methods of the protected type are visible . Our goal is to call the Class A construct later if you want to construct a derived class of Class A, and you must call the method inside Class A, even if it is a derived class, which I would like to follow, that is, not visible to derived classes .

Second, Class B inherits from Class A, which does not matter because the private part of the base class is not visible regardless of the way it is inherited. Why virtual inheritance? This later says. At the same time, Class B is defined here as a friend of Class A, then Class B will be able to directly access the private members of Class A, that is to say, the friend here, breaking the first definition of the derived class is not visible restrictions . Later, when instantiating an object using Class B, it is completely successful, and the construction of the base class part is done through friend, regardless of the inheritance mode . Class B Here is the non-inherited class that we define.

Finally, we try to get Class C to inherit Class B, so when Class C instantiates an object (or explicitly defines a constructor), it first constructs the part that belongs to Class B. Note here that if Class B is not a virtual inheriting class A, then the construction of the part of Class B is constructed through Class B, which is bound to be successful. But we do not want, therefore, the Class B virtual inheritance of Class A, when the construction belongs to the Class B part, because B virtual inherited a, then the class C directly to access a, try to construct the part of Class A, it is obvious that because access to the constructor of Class A, so C instantiation of the object failed . Thereafter, all classes that attempt to inherit Class B will fail at compile time (if C is shown to give the construct , or Class C instantiates the object).

In this case, it is not possible to inherit the success of Class B creation, it is very ingenious to apply the concept of friend class, so that only B can instantiate part of a.


If you feel that this way is too flexible and not easy to understand, then there is a simpler way. C++11 introduced the final keyword, the class that is decorated by the keyword is not inherited, and this is basically consistent with the usage in Java.

Class A final{public:a () {}};class B:p ublic A//compilation error {};


2. Define a class that can only create objects on the stack

If the situation above is understood, it should not be too difficult here. Why can I create objects on the stack only? How is it implemented? It's very simple, as long as we expose only the interfaces that can create objects on the heap. The code is as follows:

Class a{public:static * GET_A (int x) {return new A (x);} static void Delete_a (A * a) {Delete A;} private:a (int A = ten): _a (a) {}private:int _a;}; int main () {A * pa = a::get_a (9); A * PB = A::get_a (7); return 0;}


As before, both constructors and destructors are set to private (since inheritance is not involved here, private and protected are no different here), because constructors are private and cannot create objects, thus providing a static method, This method implements the acquisition and deallocation of objects on the heap through new and delete.


3. Define a class that can only create objects on the stack

If you understand the last one, you should know how to do it here. As long as we only provide the way to get the object on the stack, because the stack space is maintained by the operating system, there is no special need, the destructor is not necessary to explicitly give, the code is as follows:

Class a{public:static a get_a (int x) {return a (x);} private:a (int A = ten): _a (a) {}private:int _a;}; int main () {A PA = a::get_a (9); A PB = A::get_a (7); return 0;}


Objects that can only be created on the stack, or only on the heap, are implemented in the same way that the constructor and destructor of the class are set to private, and when my interface functions provide methods that are objects created from the heap, the class can only create objects on the heap, and when my interface functions provide objects directly from the stack, Class can only create objects on the stack. It is important to note that when you create an object on the stack, you cannot return a reference to the temporary object, which is not explained much.



------Muhuizz Finishing

This article is from the "Twilight Back" blog, make sure to keep this source http://muhuizz.blog.51cto.com/11321490/1915593

Teaches you how to create a special class 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.