I'm on a fast track C++ revision course. Trying to brush up some basic concepts. When I run the below program, I see two issues : 1. Copy CC is not called for some reason. 2. Program is crashing for some reason after the function testCC() exits.
Any help is appreciated !
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();
}