Discussion on c++ constructor problems initialization and assignment problems

  • 2020-05-10 18:36:22
  • OfStack

Default constructor (that is, constructor with no arguments)

The Default Constructor
The default constructor is the constructor used to create an object when you don't provide explicit initialization values. That is, 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

Here are two more cases

As mentioned above, there are two types of default constructor (... your own default constructor. This is a constructor that takes no arguments) :

1) One is 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 overloading to define a second constructor, one has no arguments:
Stock();

One thing to note about not using both at the same time:

You can have only one default constructor, so be sure that you 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 that takes no arguments: this refers to calling with no arguments.

The compiler automatically adds default constructor conditions: the compiler implements a constructor that does nothing, right

1. You don't have any self-defined constructors (not even copy constructors, if you define copy constructors, you have to define your own constructors)

2. There is no const and reference in the data members. Because it's going to initialize.

The copy constructor arguments must be referenced for a reason: the copy constructor arguments use reference types not to reduce one memory copy, but to avoid unbounded recursion of the copy constructor.

If it is a value, then you need to call the copy constructor one more time when passing the value

And then you have to pass it again, and then you have to adjust it again...
And then you run out of memory

On 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;// It calls the copy constructor 
return a2;
}


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

}

  copy constructor 1 calls the copy constructor four times. fun parameter is passed once, a1 (i) once, a2 (a1) once, and temporary object is constructed once during return

If the function returns an object instead of a pointer, then when return is executed, the temporary object is "copied" with the return object, and then the return statement is executed (a semicolon is encountered; The function internally created all variables destruct, out of the stack. The temporary object "constructed by assignment" is executed after the statement calling the function (a semicolon is encountered; Or after the curly brace} on the right, destruct.

Summary 1 sentence:

Temporary variables live at the statement level -- semicolon; End or end of the curly brace} on the right. At the end of the statement, the temporary variable is destructed


Related articles: