Hi, so, first, I'm making my own vector class instead of using the library. This is homework. I'm not necessarily asking for code. I have written some code, I just don't know if it is usable in the situation.
I have a Vector object(which is a double pointer array), and I'm overloading the = operator so that two objects can equal each other.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
// in main....
Vector* a = new Vector();
Vector* c = new Vector();
c[0] = a[0];
// in my .cpp
Vector& Vector::operator=(const Vector &other){
double *Equal = new double[other.size];
size = other.size;
capacity = other.capacity;
for (int i = 0; i < size; i++){
Equal[i] = other.ptr[i];
}
return *this;
}
|
Overload specifics...
Vector& operator=(const Vector &other);
/* Description:
* Assignment operator. Initializes a dynamic array of the appropriate
* size/capacity and copies data from other's array to the new array.
*
* Parameters:
* other - a constant reference to the vector that is to be copied.
*
* Post-conditions:
* ptr is initialized, and its array contains other.ptr's data,
* size=other.size, and vec_capacity=other.capacity. Note you must create a copy of other's array for the new array to get credit.
*/
Again, I understand that help is limited for hw, and that's okay. I just wanna know if I'm on the right track.
Thank you.
*edit*
There are no immediate errors with the code, compilation-wise.
*edit to include constructor and member variables*
1 2 3 4 5 6 7 8 9 10 11
|
private:
double* ptr;
int size, capacity;
// constructor
Vector::Vector(){
size = 0;
capacity = 0;
ptr = NULL;
|