빠른 진행 C++ 수정 과정을 진행 중입니다. 몇 가지 기본 개념을 닦으려고. 아래 프로그램을 실행하면 두 가지 문제가 발생합니다. 1. 복사 CC가 어떤 이유로 호출되지 않습니다. 2. testCC() 함수가 종료 된 후 프로그램이 작동하지 않습니다.복사 생성자가 여기에서 호출되지 않는 이유는 무엇입니까?
도움을 주시면 감사하겠습니다.
class A
{
public:
A()
{
this->ptr = new int[10];
}
~A()
{
delete[] ptr;
}
A(const A &obj)
{
std::cout << "Copy CC called\n";
for (int i = 0; i < 10; i++)
{
ptr[i] = obj.ptr[i];
}
}
void set()
{
for (int i = 0; i < 10; i++)
{
ptr[i] = rand() % 10;
}
}
void print()
{
for (int i = 0; i < 10; i++)
{
std::cout << ptr[i] << " ";
}
std::cout << "\n";
}
private:
int *ptr;
};
void testCC()
{
A a1,a2;
a1.set();
std::cout << "Contents of a1\n";
a1.print();
a2 = a1;
std::cout << "Contents of a2\n";
a2.print();
}
또한 [this] (http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29) – P0W