creating a copy constructor

Hi guys.

I have created a class which is basically an array that allows me to do vector algebra on the array. I have overloaded the copy constructor ass follows:


1
2
3
4
5
6
7
void myVec::operator=(myVec other){
	coor = new double[length];
	for (int i = 0; i<length; i++){
		double temp = other[i];
		coor[i] = temp;
	}
}


where length defines the length of the vector (usually 3) and coor is an array that holds the coordinates of the vector (x,y,z). I used the temp part to make sure that the vector I copy to doesn't get the pointer of the vector I'm copying from.

Nevertheless, when I change a value in the vector I copied to I can see that I have also changed the vector I copied from, what can I do to fix this?

Thanks.

Yotam.
myVec(const myVec& vec);

A copy constructor is a constructor which takes a const reference to an object of the same type as its one argument, not an overloaded operator.

By the way, why are you using an array when you could use a (typical) struct?

1
2
3
struct Vector3 {
	int x, y, z;
};
Last edited on
Thanks.

I'm not sure of how to implement your line. Should it be something like that:?

void myVec::operator=(const myVec& vec)

or should I put in my code:

myVec::myVec(const myVec& vec)

I'm using the array because I want to be able to change easily the size of the vector, the way I'm doing it now, all I need to change is a definition in the header and the only damaged function will be the scalar product calculation
That's not the copy constructor, that is the assignment operator.

Assuming your class looks something like this:
1
2
3
4
class myVec {
    double *coor;
    unsigned length;
};


An assingment operator and a copy constructor would look like this:
(Note: these functions assume that the length of the vector is never 0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
myVec::matrix(const myVec& o) : length(o.length) {    
    coor = new double[length];
    std::copy( o.coor, o.coor + length, coor );
}


myVec& myVec::operator=(const myVec& o) {
    if ( this == &o ) {
        return *this; //Self assignment : nothing to do
    }

    delete[] coor;

    length = o.length;
    coor = new double[length];
    std::copy( o.coor, o.coor + length, coor );
	
    return *this;
}


EDIT : code corrected
Last edited on
How often will you need different sizes of vectors? If it's not that often, I'd still go with structs. Otherwise, a std::vector is definitely preferable over an array.

Anyway, myVec(const myVec& vec); is the function signature for a copy constructor for your class. You're confusing it with the assignment operator.
filipe:

I don't think I'll need to change the vector size anytime soon. However, the whole class is written with the thought of using an array. I can change the class to a vector instead of array, but I can't see how I will be able to do so easily with the construct.

R0mai:

I guess I'll use the operator=. I still can't understand one part of your code though. What does the "data_" line means, shouldn't it be coor?


Thanks.
I'm sorry, data_ was a leftover from the code I copied from. I edited it.
Topic archived. No new replies allowed.