class TEST
{
Public:
Void the print ()
{
Cout & lt; <"This is a" test & lt;}
The TEST (int a=0, int b=0, int c=0) : data (a), constint (b), ref (c)
{}
The TEST (const TEST & amp; T) : data t.d (ata), constint (tc onstint) and ref (t.r ef)
{
Cout & lt; <"This is a" copy & lt;}
Private:
int data;
Float constint;
Int ref.
};
Int main (int agrc, char * argcv [])
{
The TEST t=TEST (1, four, five);
The TEST TEST=t;
}
I found in VS:
TEST t=TEST (1, four, five);//is optimized for the TEST t (1, four, five); Here I don't understand: my copy constructor did other things: in addition to copy the output, why also to optimize, is this why?
The
TEST TEST=t; Not be optimized;//here I understand, the compiler can only call copy constructor
CodePudding user response:
This just constructor overloads, with optimized hairless,CodePudding user response:
The TEST t=TEST (1, four, five); This sentence did not trigger the copy constructor,But use your own definition of constructors,
The TEST (int a=0, int b=0, int c=0) : data (a), constint (b), ref (c)
{}
CodePudding user response:
Standard allowed, copy constructionThe TEST t=TEST (1, four, five); This is equivalent to
The TEST t (TEST (1, four, five));
Because used to initialize the t is a temporary object, after completion of initialization is discarded, according to standard allow direct according to
The TEST t (1, four, five); To t way of construction, and is "even though that could lead to change the behavior of the side effects", that is to say, even if you have your own copy, copy constructor is still allowed to be ignored,
In order to adapt to the rules, you can write as much as possible does not depend on the optimization of program logic,
There are other VS not standard, is that if the TEST (const TEST & amp;) Is declared as private or delete function, v will completely ignore check accessibility, and standards do not do this optimization will check accessibility,
CodePudding user response:
The TEST t=TEST (TEST (1), four, five);CodePudding user response:
C + + compiler is very smart, some not used, can optimize away