#include <iostream>
#include <utility>
usingnamespaceStd
classa{
Private:
intData//Data
int*pi;//Point to data
Public:
//Suppress implicit conversions
A () {
data =0;
PI = &data;
}
~a () {
data =-999999;
PI = nullptr;
}
ExplicitAinti):d ata (i) {
cout <<"Normal constuctor!"<< Endl;
PI = &data;
}
AConstA &a) {
data = A.data;
cout <<"Copy constructor!"<< Endl;
PI = &data;
}
A (a &&a) {
cout <<"Move constructor!"<< Endl;
//move A.pi directly to Pi
PI = A.pi;
data = A.data;//Modify Source Pi
A.pi = nullptr;
A.data =0;
}
Aoperator+(ConstA &a) {
A Temp (data + a.data);
cout << Endl <<"operator+ called!show temp!"<< Endl;
Temp.show ();
cout << Endl;
returnTemp
}
voidShow ()Const{
cout <<"pi="<< Pi <<"data="<< data << Endl;
}
};
classB
{
Private:
A _a;
Public:
//B (A aa): _a (aa) {
// }//when you get here, AA and _a still exist!! AA wasted, White dead!
//************************ most efficient ***********************************
BConsta& aa): _a (aa) {
}//using a copy in main to construct _a, no waste
//*********************most efficient*****************************
//B (A aa) {
//_a = Std::move (aa);//_a in vain to construct it again!
// }
~b () {}
};
//vs can press the TAB key like Eclipse!!!!!!!
intMain ()
{
inti = About;
A A (Ten);
b b (a);//Copy Construction a
}
I would have guessed that there would be a move constructor compiler optimizer, but the Const & could also avoid redundant constructor calls, and I think more
C + + constructor The most efficient way to initialize object members