Constructors and copy constructors

Source: Internet
Author: User

  1. constructor Function

    constructors, destructors, and assignment functions are the most basic functions of each class.

    For any class A, if you do not want to write the above functions, the C + + compiler will automatically generate four default functions for a, for example:

    1  A (void//  default parameterless constructor 2  A (const//  default copy constructor 3  ~a (void//  default destructor 4  A & operate = (const//  The default assignment function

    The default constructor and default destructor are only responsible for object creation and destruction, not initialization of objects and cleanup of resources.

    In C + +, the constructor ensures initialization of the object, and if the class has a constructor, the compiler automatically calls the function when the object is created.

    Constructor Features:

    • Same name as Class
    • No return value (consistent with destructor)
    • constructors can be overloaded, which constructors are called by the arguments
      1 classA2 {3        Public:4           inti,j;5           6A (): I (0), J (0) {};//Constructor One: Initialize the list with a constructor function7AintIiintIJ);//constructor Two: assigning values using Parameters8 };9  TenA::a (intIiintIJ) { Onei =II; Aj =IJ; -}

      The constructor initializes a data member with an initialization list, which in some cases is the only option:

      Initialization of const and reference type objects in a class can only be done with an initialization list and cannot be assigned a value operation

      1 classA2 {3      Public:4AintII);5     Private:6         inti;7         Const intci;8         int&ri;9 };Ten  OneA::a (intII) A { -i = II;//OK -CI = II;//A const object cannot be assigned a value theRI = i;//assignment to Ri but object not bound - } -  - //The correct wording is as follows: +A::a (intII): I (ii), CI (i), RI (ii) {}

      The default constructor is the constructor that is called when the initialization is not explicitly provided constructor. It is defined by a constructor that takes no arguments, or a constructor that provides default arguments for all of the formal parameters. If you define a variable for a class, the default constructor is used when you do not provide an initializer.  

    • 2. Copy Constructors

      Characteristics:

      • Only a single parameter, the parameter is a reference to the object of this class type (decorated with const)

      (1) If no copy constructor is defined, the compiler will synthesize the copy constructor

      The function behavior is to initialize a member by one and then initialize the new object to a copy of the original object. An exception is the array member, which copies the entire array.

      (2) Custom copy constructors

      A (const a&);

      Most classes should define copy constructors and default constructors.

      (3) Prohibit copying

      If a class needs to disallow assignment, and explicitly declare the copy constructor private, the user code is not allowed to replicate objects of that class type.

      However, friends and members of a class can still replicate, and it is forbidden to declare a copy constructor private and not define it.

Constructors and copy constructors

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.