S
silversurfer2025
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?
myVector myVector operator +(myVector val){
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...
Thanks for your hints,.. I am looking forward to hearing from you.
Greetings
Tim
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?
myVector myVector operator +(myVector val){
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...
Thanks for your hints,.. I am looking forward to hearing from you.
Greetings
Tim