Copy Constructors/Destructors, Dynamic Arrays, & Assignment
Hello, I'm trying to create a copy constructor, destructor, and a swap function for a class that creates a dynamic array like:
Code:
class A
{
public:A(....)
{
dArray = new int [size];
}
A(const A& other)
:size(other.size)
{
dArray = new int [size];
for(int i = 0; i < size; i++)
dArray[i] = other.dArray[i];
}
~A()
{
delete [] dArray;
}
int& operator=(const int& right)
{
if (this != &right)
{
A temp(right);
swap(temp);
}
}
void swap(A& other)
{
int tmp = size;
size = other.size;
other.size = tmp;
int* tmpPointer = dArray;
dArray = other.dArray;
other.dArray = tmpPointer;
}
private:int* dArray;
int size;
};
Am I going in the right direction with this or am I making a bunch of dangling pointers and memory leaks? I'd appreciate some guidance if I am.
Thanks.