Home > Back-end >  Please ask next everybody A, A=10. How is a form of initialization object?
Please ask next everybody A, A=10. How is a form of initialization object?

Time:11-26

A custom categories:
Class A
{
Private:
The int value.
Public:
A (int n)
{
value=https://bbs.csdn.net/topics/n;
}
}
Int main ()
{
A, A=10.//this way of initialization is what principle?
}
Thank you!

CodePudding user response:

This belongs to the assignment, or copy constructor,
The compiler provides the default copy constructor,

CodePudding user response:

Not copy structure, is call it directly with the constructor,

CodePudding user response:

When no copy elision optimization, implicit call first with A (int), then calls A (const A&)

A copy elision optimization, implicit call only use A (int)

CodePudding user response:

reference SDGHCHJ reply: 3/f
no copy elision optimization, implicit call with A (int), and then call A (const A&)

A copy elision optimization, implicit call only use A (int)

all the object from another object of the same type (in direct initialization or duplicate initialization) initialization time, calls the copy constructor (unless the overload resolution chose the better matching or the call is eliminated), including

Initialization: T=b; Or T a (b); Of b type T;
Function argument passing: f (a); , of which a type of T and f Ret f (T, T);
Function returns: T f () function in such as internal return a; , of which a type T, it did not move constructor
remember is the same type

CodePudding user response:

references in 4th floor, the truth is more important than right or wrong response:
Quote: refer to the third floor SDGHCHJ response:
no copy elision optimization, implicit call with A (int), and then call A (const A&)

A copy elision optimization, implicit call only use A (int)

all the object from another object of the same type (in direct initialization or duplicate initialization) initialization time, calls the copy constructor (unless the overload resolution chose the better matching or the call is eliminated), including

Initialization: T=b; Or T a (b); Of b type T;
Function argument passing: f (a); , of which a type of T and f Ret f (T, T);
Function returns: T f () function in such as internal return a; , of which a type T, it did not move constructor
remember is the same type




https://bbs.csdn.net/topics/397142613

- fno - elide - constructors

CodePudding user response:

Probably understand what you mean, is it not optimize 1 when the parameters used to implicit first constructs A class A (temporary objects), and then through the copy copy structure to A, you have to optimize constructor is implicit call directly with direct A, I understand it right?

CodePudding user response:

 # include & lt; Iostream> 

Class A
{
Public:
A (int n) {
Value=https://bbs.csdn.net/topics/n;
}

Friend STD: : ostream & amp; The operator & lt; <(STD: : ostream & amp; Out, const A& A) {
The out & lt; Return the out;
}
Private:
The int value.
};

Int main (int arg c, char * argv [])
{
A, A=10.
STD: : cout & lt; return 0;
}

1. The first test explicit constructor
 # include & lt; Iostream> 

Class A
{
Public:
Explicit A (int n) {//Notice here!!!
Value=https://bbs.csdn.net/topics/n;
}

Friend STD: : ostream & amp; The operator & lt; <(STD: : ostream & amp; Out, const A& A) {
The out & lt; Return the out;
}
Private:
The int value.
};

Int main (int arg c, char * argv [])
{
A, A=10.
STD: : cout & lt; return 0;
}
//the Compiler complained
//error: conversion from "int" to non - scalar type 'A' requested
//A, A=10.

Shows that there are at least implicit conversion
2. The test again, banned the copy constructor
 # include & lt; Iostream> 

Class A
{
Public:
A (int n) {
Value=https://bbs.csdn.net/topics/n;
}

A (const A&)=the delete;//Notice here!!!!!!

Friend STD: : ostream & amp; The operator & lt; <(STD: : ostream & amp; Out, const A& A) {
The out & lt; Return the out;
}
Private:
The int value.
};

Int main (int arg c, char * argv [])
{
A, A=10.
STD: : cout & lt; return 0;
}
//the Compiler complained
//main. CPP: In function 'int main (int, char * *) ":
//main. CPP: and: error: the use of does the function 'A: : A (const A&) '
//A, A=10.
//^ ~
//main. CPP: 10:5: note: declared here
//A (const A&)=the delete;

The instructions to copy constructor

Thus, A A=10, the compiler will generate code for you:
 A temp (10);//int the convert to class A 
A A (temp);//copy constructor


3. To disable the copy the assignment the construct is useless, because the initialization time use is the copy constructor
 # include & lt; Iostream> 

Class A
{
Public:
A (int n) {
Value=https://bbs.csdn.net/topics/n;
}

A& Operator=(const A&)=the delete;//Notice here!!!!!!

Friend STD: : ostream & amp; The operator & lt; <(STD: : ostream & amp; Out, const A& A) {
The out & lt; Return the out;
}
Private:
The int value.
};

Int main (int arg c, char * argv [])
{
A, A=10.
A ano_a (20);
Ano_a=a;
STD: : cout & lt; return 0;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related