I have below code where I have a class with an array which is created by a pointer and another simple integer value.
Since I am creating the int array with a pointer, thus I need to implement a copy constructor to handle the pointer copy when I assign one object of the class to another.
Here is the class declaration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
class array {
private:
int *p;
int size;
public:
array(int len) {
p = new(nothrow) int[len];
if(p != nullptr)
size = len;
else
{
cout<<"Allocation failure";
exit(EXIT_FAILURE);
}
}
array(array &a); //copy constructor
void put(int i, int j) { if(i>=0 && i<size) p[i]=j;}
int get(int i) {
if(i>=0 && i<size) return p[i];
else return -1;
}
~array() {
cout<<"destrying..."<<endl;
delete [] p;
}
};
|
Then I implement the copy constructor like below where I manually implement the memory allocation of the array in the new object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
array :: array( array &a)
{
int i;
p = new(nothrow) int[a.size];
if(p == nullptr)
{
cout<<"Allocation failure"<<endl;
exit(EXIT_FAILURE);
}
for(i=0; i<a.size; i++)
p[i]=a.p[i];
//why explicitly writing this line is necessary?
//size = a.size;
}
|
However, as mentioned in the commented line, as long as i explicitly don't copy the "size" variable, c++ doesn't automatically copy the value from old object to new object.
Example, below is how I call them from main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
int main()
{
array num(10); //create a 10 size array
for(int i=0; i<10; i++) // assign 10 values into it
num.put(i, i);
for(int i=0; i<10; i++) // print the values of num
cout<<num.get(i)<<endl;
// creating a new array type object named "X" where
// values from obj "num" being copied
array x(num);
for(int j=0; j<10; j++) //print values of X obj
cout<<x.get(j)<<endl;
return 0;
}
|
Here the statement
array x(num) leads to properly copying all values of p from
num to
x.
But as long as I don't explicitly write
size = a.size inside the copy constructor, variable
size inside
x gets 0, not 10.
Does it mean that if I implement a copy constructor, then I have to manually handle copying each member from source object to destination object?
I tried to google the problem, but may be my keywords weren't good enough so I didn't get a satisfactory answer to my question. Assuming this is a trivial question, I appreciate if anyone links any existing thread which addresses my question.
Thanks in advance.