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:
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?
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
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.
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?