사용자가 배열에 배치 된 임의의 숫자를 쓰고 그에게 표시되는 C++에서 응용 프로그램을 만들었고 복사 생성자를 사용하여 입력 된 시간이 다시 표시됩니다 . 하지만 여기서 문제는 복사 기능이 실행되지만 정보를 표시하지 않는다는 것입니다. 당신은 이동 생성자에서 데이터 멤버 세를 복사하는 것을 잊었다C++ 복사 생성자 런타임 오류
#include <iostream>
#include <string>
using namespace std;
class Identity{
protected:
int* arry_int;
int ages;
Identity(){} //private default constructor
public:
Identity(int repeated_number){ //overloaded constructor
ages = repeated_number ;
arry_int = new int [ages];
}
void getAge(){ //getting age form the user
for (int i = 0; i < ages; i++){
cout << "Enter age[" << i << "]: ";
cin >> arry_int[i];
}
}
void printAge(){
cout << "Friend's ages" << endl;
cout << "-----------------" << endl;
for (int i = 0; i < ages; i++){
cout << arry_int[i] << endl;
}
}
//move copy constructor
Identity(Identity&& cpy){
cout << "Declaring move constructor" << endl;
arry_int = cpy.arry_int;
cpy.arry_int = NULL;
}
//move assignment operator
Identity& operator=(Identity&& cpy){
cout << "Declaring move assignment operator" << endl;
if (this != &cpy){
delete arry_int;
arry_int = cpy.arry_int;
cpy.arry_int = NULL;
}
return *this;
}
~Identity(){
delete arry_int;
}
};
int main(){
string nemesis;
Identity iden(5);
iden.getAge();
iden.printAge();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Enter your nemesis name: " << endl;
getline(cin,nemesis);
//nemesis stealing your identit
Identity stolen(move(iden));
cout << "Now " << nemesis << " stole your friend's age and used it against you" << endl;
stolen.printAge();
system("pause");
return 0;
}
복사 생성자와 이동 생성자는 고유합니다. 용어를 섞어 쓰지 마십시오. 그것은 단지 다른 사람들을 혼란스럽게합니다. – Brian