Home > Back-end >  New one, ask your bosses, compiled in vs2019, run time are obtained.
New one, ask your bosses, compiled in vs2019, run time are obtained.

Time:10-03

# include
using namespace std;
Class A {
Public:
A (int n=10) {cout<& lt;" The default structure "& lt; /* if commented out here below a copy of the function, to run the program when he would call the destructor twice, just call a default constructor, the operation is in vs2019, put a breakpoint at he gave up his own destructor twice, the following comments the result to invoke a constructor, a destructor function called, I put this on the web page for the online compiler to run, no matter note not comment below a copy of the function result is called a constructor a destructor, to this confused, I don't understand what meaning is that the add function travels, according to the teacher the inscribed, writing is not very full, with some parameters,
*/
A (const A& A) {cout<& lt;" Copy "& lt;
~ (A) {cout<& lt;" The destructor "& lt; };

Void the add (A) A {}

Int main ()
{
Add (A (10));
return 0;
}

CodePudding user response:

Should be arguments and parameters are calling the destructor, and copy is quite so to construct A reference, the add function is an empty function, only defines an object parameter A

CodePudding user response:

reference 1st floor qq_46221910 response:
should be arguments and parameters are calling the destructor, and copy is quite so to construct A reference, the add function is an empty function that only defines an A object parameter

Did not understand a copy function that, with only call a destructor, do not add up twice, the constructor I add shorter step by step, go up only once, when I went in the two compilers, the results of a and v, a web page and I said, if this is related to the compiler,

CodePudding user response:

Personally think that
=========
If no class type (struct, class or union) provide any user to define the copy constructor, the compiler will always declare a copy constructor, as a kind of explicit inline public members, when the following are true, the implicit declaration form of copy constructor has T: : T (const T&) :

T each directly with the virtual base class B has a copy constructor, the parameters for the const B& Or const volatile B&;
T each class type or class non-static data members of an array of type M own copy constructor, the parameters for the const M& Or const volatile M& ,
Otherwise, copy constructor is implicitly declared T: : T (T&) , (note that because of these rules, the copy constructor implicitly declared cannot be bound to the volatile lvalue argument),
================
The above from cpprefrence

Try the following code
{
 class BPublic: 
(B) {}
B (const B& ) {}
};
Class A {
B, b1;
Public:
A (int n=10) {cout & lt; <"The default structure & lt;" /* if commented out here below a copy of the function, to run the program when he would call the destructor twice, just call a default constructor, the operation is in vs2019, put a breakpoint at he gave up his own destructor twice, the following comments the result to invoke a constructor, a destructor function called, I put this on the web page for the online compiler to run, no matter note not comment below a copy of the function result is called a constructor a destructor, to this confused, I don't understand what meaning is that the add function travels, according to the teacher the inscribed, writing is not very full, with some parameters,
*/

~ (A) {
Cout & lt; Cout & lt; <"Of" the destructor & lt; }
};

Void the add (A) A {
}

Int main ()
{
Add (A (10));
return 0;
}

CodePudding user response:

Notice that I stick to that part of the, when you do not provide a constructor, the compiler provides the constructor of the form a T (T&) Not T (const T&) Because the parameter is T& Not directly binding temp so to reproduce,
While you provide the T (const & amp; T) will cause copy to eliminate, specific see
 https://zh.cppreference.com/w/cpp/language/copy_elision 

Because add A field to A B, B with T (const T&) In the form of A copy constructor, so A copy implicit structure will be A (const A&) Form,
So, will cause copy to eliminate,

CodePudding user response:

references in 4th floor, the truth is more important than right or wrong response:
notice I stick to that part of the, when you do not provide a constructor, the compiler provides the constructor of the form of a T (T&) Not T (const T&) Because the parameter is T& Not directly binding temp so to reproduce,
While you provide the T (const & amp; T) will cause copy to eliminate, specific see
 https://zh.cppreference.com/w/cpp/language/copy_elision 

Because add A field to A B, B with T (const T&) In the form of A copy constructor, so A copy implicit structure will be A (const A&) Form,
So, will cause a copy to eliminate,

Please you bother, thanks.
  • Related