2.10 C + + uses constructors to restrict the creation of objects

Source: Internet
Author: User

Reference: http://www.weixueyuan.net/view/6342.html

Summarize:

Restricts the creation of objects. Restricting the creation of objects can be created as we need them, rather than creating objects at will.

Any constructor is declared in the class, and the constructor is no longer automatically generated by the system.

A constructor declared as a private property, like a member variable or a normal member function declared as a private property, is also inaccessible outside the class 

In Example 1 of the previous section constructor, we mentioned that if you do not declare the default constructor book (), and only declare the parameter constructor book (char *a, double p), the statement book Alice is unable to create the object. The reason why you cannot create is explained in the previous section, and this section will not be mentioned again.

However, this attribute of the constructor of a class can be used to restrict the creation of objects.

[Example 1] or using the book class to illustrate the content of this section:

  1. Class Book
  2. {
  3. Public:
  4. void setprice(double A);
  5. Double GetPrice();
  6. void settitle(Char* a);
  7. Char * gettitle();
  8. void display();
  9. Private:
  10. double price;
  11. Char * title;
  12. };

The first thing to know is that we need to restrict the creation of objects in what circumstances. Restricting the creation of objects does not mean that we want to prohibit the creation of objects, but restrict them by restricting the creation of objects as we need them, rather than creating them arbitrarily.

For example 1, we define a book class, when declaring the object is naturally to describe a specific books, this time if we are directly to adopt A, such a way to create an object A, compilation is certainly not a problem, after all, the system will automatically generate a default constructor. But what does the object a that you create can represent? Which book does it refer to? Not at all clear!

If we call the A.display () function directly at this time, then what happens is not very good, after all, A.price and a.title are not initialized. This is a more dangerous procedure. But surely someone would say, is it possible to assign a value to two member variables through the a.setprice and A.settitle functions? Yes, it does, but what if it's forgotten?

For such a class, if the designer of the class let book A, such a way to create the object is not established, and each time you create the object must give the price and title of the books to assign value, so that the problem does not solve it?

Can such an idea come true? The answer is yes. We can do this by constructing a function. Look at some examples of the following classes.

[Example 2] does not declare a default constructor:

  1. Class Book
  2. {
  3. Public:
  4. book(char *a, double P);
  5. void setprice(double A);
  6. Double GetPrice();
  7. void settitle(Char* a);
  8. Char * gettitle();
  9. void display();
  10. Private:
  11. double price;
  12. Char * title;
  13. };


[Example 3] declares the default constructor as private:

  1. Class Book
  2. {
  3. Public:
  4. book(char *a, double P);
  5. void setprice(double A);
  6. Double GetPrice();
  7. void settitle(Char* a);
  8. Char * gettitle();
  9. void display();
  10. Private:
  11. Book(){}
  12. double price;
  13. Char * title;
  14. };

In Example 2, we declare a parameter constructor book (char *a, double p) in the class, so that the default constructor is not automatically generated by the system, that is, the constructor of book () {} is not generated, so that the object book A is created, and a compilation error occurs. To create the object correctly, you must initialize it, such as book Alice ("Alice in Wonderland", 29.9);.

In addition to this approach, the practice of example 3 is also feasible, we have the default constructor is declared and defined, the most important thing is to set it to private property, that is, can not be accessed outside the class, so book A; , if you want to declare an object, you must still declare the object by using the parameter constructor book (char *a, double p) of the public property.

Based on the above examples, we summarize the system not to automatically generate default constructors as follows:

    • The default constructor is explicitly declared in a class, and the default constructor is no longer automatically generated by the system, regardless of whether its property is set to public, private, or protected.
    • Class explicitly declares any other constructor that is not a default constructor, the default constructor is no longer automatically generated by the system.


In fact, these two points can be summed up as a point, that is, when the class is declared any one of the constructors, the system will no longer automatically generate constructors. In Example 2, we use the second case to avoid the creation of the default constructor, which limits the arbitrary generation of class objects.

In C + +, the use of constructors to restrict the creation of class objects is already evident in the intention that the program designer should be able to initialize the object correctly when using the class.

A constructor declared as a private property, like a member variable or a normal member function declared as a private property, is also inaccessible outside the class, and we can use this subtly to restrict the arbitrary creation of class objects.

2.10 C + + uses constructors to restrict the creation of objects

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.