Excellent article on unreasonable design in C ++ Constructor (1)

Source: Internet
Author: User

InC ++Medium,ConstructorIt is a special function called when the component object is used to initialize the object, so that the object can be in a reasonable State before it is used. However, the design of constructor functions is not perfect, and there are even some unreasonable features. For example, the condition that the constructor name and class name are the same is limited. These features are worth noting when constructing the C ++ compiler. Also, these features should be avoided in the future when C ++ standards are revised or other object-oriented design languages are formulated. Some solutions are also proposed here.

 

In C ++, any class has at least one constructor, even when no constructor is declared. When an object is declared or dynamically generated, these constructors are called. The constructor does a lot of invisible work, even if there is no code in the constructor, such work includes allocating the memory of the object and initializing the members by assigning values. The name of the constructor must be the same as the name of the class, but there may be many different overloaded versions to provide. The parameter types are used to distinguish the constructor versions. Constructors can be explicitly called through user code, or implicitly inserted when the Code does not exist by compiling programs. Of course, explicit calling through code is a recommended method, because the implicit calling effect may not be expected, especially in dealing with dynamic memory allocation. The Code calls a unique constructor through parameters. The constructor does not return values, even though it can return statements in the function body. Each constructor can instantiate an object in a different way. Because each class has a constructor, at least the default constructor, each object uses the constructor before use.

Because a constructor is a function, its visibility is nothing more than three types of public, private, and protected. Generally, constructors are declared as public. If the constructor is declared as private or protected, Object Instantiation is restricted. This is effective in preventing classes from being instantiated by others. The constructor can have any C ++ statements. For example, a print statement can be added to the constructor to indicate the position of the call.


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.