C + + constructor problems, initialization and assignment issues

Source: Internet
Author: User

Default constructor (that is, a constructor without parameters)

The Default Constructor
The default constructor is the constructor used to create a object when you don't provide explicit initialization values. That's, it's the constructor used for declarations like this:

Stock Stock1; Uses the default constructor

1. Automatically generated by the compiler

2, by our own definition of

There are two more things here.

It says, the default constructor has two kinds (... your own default constructor. This was a constructor that takes no arguments):
1) One is to provide default values for all the arguments to the existing constructor:
Stock (const char * co = "Error", int n = 0, double PR = 0.0);
2) The second is-to-use function overloading to define a second constructor, one of the has no arguments:
Stock ();
One thing to note is that both cannot be used at the same time:
You can has only one default constructor and so is sure that don't do both. (with early versions of C + +, you could use only the second method for creating a default constructor.)
This is a constructor the takes no arguments: This refers to the call without parameters.

The compiler automatically adds the criteria for the default constructor: The compiler implements the constructor that actually does nothing.

1. There is no constructor of its own definition (that is, the copy constructor does not work, and if you define the copy constructor yourself, you must define the constructor yourself)

2 . There are no const and reference in data members. --because you want to initialize.

The parameter of the copy constructor must be the reason for the reference: The parameter of the copy constructor uses the reference type not to reduce a memory copy, but to avoid the duplication of the constructor's unrestricted recursion.

If it's a value, it's going to have to be transferred again when the value is passed. copy constructor
And then you have to pass the value again, again ....
And then you don't have enough memory to be

About the difference between the assignment = = function and the copy constructor:

#include <iostream>
using namespace Std;
Class A
{public:
int i;
A (const a& a)
{i=a.i;
cout<< "Copy is Build" <<endl;
}
explicit A (int y)
{i=y;
}
};
A fun (a i)
{A A1 (i);
A a2=a1;//actually calls the copy constructor
return A2;
}

int main ()
{A A (1);
Fun (a);

}

The copy constructor calls four copy constructors altogether: The fun parameter is passed once, A1 (i) once, A2 (A1) once, return time constructs the temporary object once

If the function returns an object instead of a pointer, then when the return is executed, the returned object is used to "copy construct" the temporary object, and then the return statement executes (with a semicolon;) all the variables created inside the function are destroyed and stacked. A temporary object that is "assigned" is then destroyed after the statement that called the function finishes executing (a semicolon is encountered, or the right curly brace}).

A summary sentence:

The temporary variable's survival range is the statement-level-semicolon; end or the right curly brace} ends. After the statement is finished, the temporary variable is refactored ~

C + + constructor problems, initialization and assignment issues

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.