Your copy constructor should make a copy the object passed to it.
Instead it does this;
DynArray::DynArray(const DynArray& origClass) // copy constructor
{
cout << "Copy Constructor Called." << endl;
int* arrayVector = new int[2];
*arrayVector = *(origClass.arrayVector); // missing ; added here
return;
}
Ignoring output to cout, the two obvious problems are that arrayVector is dynamically allocated with two elements, regardless of how many elements there are in origClass.arrayVector.
The statement int *arrayVector = *(origClass.arrayVector) also does not copy an array at all.
- First of all it creates a pointer named
arrayVector (which is unrelated to the member of DynArray, despite having the same name).
- Then it copies the first element of
origClass.arrayVector (a
single int) to the first element of arrayVector. So it is
functionally equivalent to arrayVector[0] =
origClass.arrayVector[0]. That's the way pointers work.
- Lastly, when the constructor returns, the
arrayVector declared ceases to exist, because if is local to the body of the constructor. A consequence is that the dynamically allocated memory is leaked (no variables, or members of your DynArray class, refer to it at all, so there is no way in standard C++ to find that memory in subsequent code).
Lastly, your copy constructor does not copy any other members of origClass (arraySize, arrayCapacity, newNum, pushCounter, atNum, i)
to the new object being constructed.
Instead, what you (typically) need to do is copy ALL of of the members, not just one of them. And, if one of the members being copied is a dynamically allocated array - as in your case - the constructor needs to first construct a new array, and THEN copy all elements to it - one at a time, in a loop of some form.
For example, you could do
DynArray::DynArray(const DynArray& origClass) // copy constructor
{
arrayVector = new int[origClass.arrayCapacity];
This actually modifies the arrayVector member of the DynaArray (rather than declaring a local variable). It also makes that pointer point at a dynamically allocated array of the same size as in origClass.
What this hasn't done is copy data from origClass.arrayVector. AS I noted above, this cannot be done using *arrayVector = *(origClass.arrayVector). Instead the constructor needs to do this
for (int i = 0; i < origClass.arrayCapacity; ++i)
arrayVector[i] = origClass.arrayVector[i];
This step is essential to ensure ALL the elements of origClass.arrayVector are copied. There are variations of HOW this is done (e.g. using pointer syntax rather than array syntax) but the above will suffice.
Then it is necessary to copy all of the other members from origClass.
arraySize = origClass.arraySize;
arrayCapacity = origClass.arrayCapacity;
newNum = origClass.newNum;
pushCounter = origClass.pushCounter;
atNum = origClass.atNum;
i = origClass.i;
} // the body of the constructor ends here.
As an aside, it strikes me you have given your DynArray a number of members it doesn't need. Some of those members are only needed as temporaries in particular member functions (e.g. loop counters). Doing that means EVERY instance of DynArray has its own copy of those variables. That is unnecessary - they would be better off being local variables, declared in each function where they are needed.
Now, the above will (sort of) work, but needs to be improved. It is generally better to use initialisers in constructors where possible, and have the constructor body only do things that can't be done in initialisers. There are various advantages to doing this, but you can do homework to find out what they are.
In the end, therefore, I would implement the copy constructor as
DynArray::DynArray(const DynArray& origClass) : // initialiser list begins here
arrayVector(new int[origClass.arrayCapacity]),
arraySize(origClass.arraySize),
arrayCapacity(origClass.arrayCapacity),
newNum(origClass.newNum),
pushCounter(origClass.pushCounter),
atNum(origClass.atNum),
i(origClass.i)
{
// body of constructor starts here
for (int i = 0; i < origClass.arrayCapacity; ++i)
arrayVector[i] = origClass.arrayVector[i];
}
Not withstanding my previous aside that you have unnecessary members of the class, the initialiser list here still copies ALL of your DynArray members.
Note that the loop to copy array elements is in the body of the constructor, since that sort of thing cannot be done readily in an initialiser list.
Also note that the variable i (the loop counter in the constructor body) has no relationship to member of DynArray named i.
Lastly, the member of DynArray
int arrayVector [];
would be better declared as a pointer. It is not actually an array.
int *arrayVector;
push_backfunction does when it allocates new memory. Also, you can removenewNumfrom your class declaration, it's a parameter passed to your function and does not need to be declared there. RemovepushCounteras well and usearraySize.pushCounteris useless since it does the exact same thing asarraySize. You have a member calledarrayVectorthat stores the pointer to the dynamic array, but you then declare local variables with the same name in your constructor andpush_backfunctions. You should also learn about constructor member initialization lists instead of assigning stuff to member variables.newNumandatNumare member function arguments, not class members.