In the course of my programming I have created a new class called Vector (yes I know that vector.h exists, but I like my version better, or at least I have until now). As part of my class, I have overloaded the = to use as an assingment function, as in A=B.
Here's the code:
Vector& Vector:: operator=(const Vector& u)
{
if (this!=&u) // check for self assignment
{
delete[] v;
double *newdata=new double[u.L];
for (int i=0; i<u.L; i++) {newdata[i]=u(i);}
v = newdata;
L = u.L;
}
return *this;
}
By why of explanation, u.L returns the length of the Vector. v is the hidden array within the Vector that holds its contents. The element of a Vector is accessed using Vector(element number).
The interesting thing is, this code compiles and runs just fine with the Borland C++ compiler, and I have been using this code for years without problems. I recently updated to the MinGW compiler. The code still compiles. However, when I run the program this assignment function causes the program to crash, with the console error message:
Process returned -1073741795 (0xC000001D) execution time : 3.260 s
Now, the really weird part is that in tracking down this error I added an output to the function as follows:
for (int i=0; i<u.L; i++) {newdata[i]=u(i); cout << newdata[i];}
When I add this, the function and the program compiles and runs just fine.
Now, the question is: WHY?
What have I done wrong?
What am I missing?
Why does the function work properly with the useless cout << newdata[i]; added, but crashes without it?
Why does it work as expected in the Borland C++ compiler but not in MinGW?
I suppose the answer to my last question is that that there is something new to the more modern MinGW compiler, but I do not know what that might be. Still, isn't code using older conventions (if that is what I am doing) still supposed to work properly?