// method. The line ret[i] = pFeatVec[i] + val[i]; is working properly,
// adds them into the local variable.
Aah, OK, so it is save to give back "result" because it is copied.
Thanks for the info, I think that I got it now (giving back references
and pointers to local variables is the problem, not giving back the
objects themselves... thanks! Finally came the general insight! Thanks
for that).
If the local object is copied by returning it from the method, do I
need to write a copy-constructor as well? I am getting athe following
compiler-error from this line in the mai() method:
myVector r = fv1 + fv2;
error:
main.cpp:123: error: no matching function for call to `myVector::
myVector(myVector)'
myVector.h:26: error: candidates are:
myVectorr::myVector(myVector&)
What's that supposed to mean?
myVector result;
//add elements here,...
return result;
This was just a way to let everything be shorter.. It should have only
shown that I have a myVector object within the method, then change this
object and give it back in the end.
The above operator doesn't use any pointers or references.
I know, I merely thought of using them because I thought that I could
not return a local variable.
Rolf Magnus schrieb:
silversurfer2025 wrote:
Hello,
I would like to overload the operators *, + and = in a class, which is
used much like a vector (such as in linear algebra). For this, I need
to be able to add featureVectors to each other, etc just like you do
with real vectors. For instance, sth like the following should be
possible:
myVector x,y,z;
//initialize vectors here...
x = y+z;
If my understanding is right (and please correct me if I am not), I
have to overload the operators in the class myVector. And here comes
the tricky part: If I write a method which should do exactly this, as
in the following example, I am giving back a local variable, which
should be destroyed when out of scope..or am I not?
You are not. You're giving back a copy of the local variable.
myVector myVector operator +(myVector val){
What's that supposed to mean?
myVector result;
//add elements here,...
return result;
}
Is this valid or not? I thought of using pointers, like *myVector as
return, but then I get trouble with things like x = y+z+v etc.
Sorry that I am currently asking questions, which might seem more than
basic to most of you, but I am fairly new to C++ after using Java for
years and I am easily confused with pointers, references, etc...
The above operator doesn't use any pointers or references.