overloading +, * and = for complex objects

  • Thread starter silversurfer2025
  • Start date
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
 
R

Rolf Magnus

silversurfer2025 said:
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.
 
S

silversurfer2025

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

silversurfer2025

Maybe it is good to have the method as well:
// quick aside: myVector needs a dimension for the constructor and
'dimension' is known in the
// method. The line ret = pFeatVec + val; is working properly,
it copies the elements and
// adds them into the local variable.

myVector myVector::eek:perator +(myVector& val) {
myVector r(dimension);
for(int i=0; i<dimension; i++) {
r = pFeatVec + val;
}
return r;
}
 
R

Rolf Magnus

silversurfer2025 said:
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).

Yes, that's right.
If the local object is copied by returning it from the method, do I
need to write a copy-constructor as well?

Only if you want to do something else than the compiler-generated copy
constructor would do. It does a memberwise copy of the object.
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&)

Probably you are trying to copy a constant object, but by your declaration,
you told the compiler that the original object gets modified by your copy
constructor. Try to declare it as:

myVector::myVector(const myVector&)
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.

I know, I merely thought of using them because I thought that I could
not return a local variable.

It's just the other way round, but you already know that now :)
 
S

silversurfer2025

I tried adding const to the copy-constructor, but it does not work
either (although there is another error message now:

myVector.cpp: In copy constructor `myVector::myVector(const
myVector&)':
myVector.cpp:34: error: passing `const myVector' as `this' argument of
`int myVector::size()' discards qualifiers

I guess this is because I am using an instance-method of the
to-be-copied-object in the copy constructor...
Probably you are trying to copy a constant object, but by your declaration,
you told the compiler that the original object gets modified by your copy
constructor.
It is true that I would like change the original object by this method.
But this should be no mistake as such shouldn't it? Strings can do
exactly the same. Thus, what I need should also be implemented in the
std::string class because you can say:
string s1("ladida");
string s2("testing"); and then
s1 = s1 + s2;

thanks a lot for your help..
Tim
 
R

Rolf Magnus

silversurfer2025 said:
I tried adding const to the copy-constructor, but it does not work
either (although there is another error message now:

myVector.cpp: In copy constructor `myVector::myVector(const
myVector&)':
myVector.cpp:34: error: passing `const myVector' as `this' argument of
`int myVector::size()' discards qualifiers

I guess this is because I am using an instance-method of the
to-be-copied-object in the copy constructor...

Well, not exactly. It's because you're using a non-const member function on
a const object. You have to look up how to do const correct programming.
size() probably doesn't change the object, but unless you make it const,
the compiler doesn't know that, and so it assumes you are trying to modify
a constant object, which is of course not allowed. Try to declare your
size() function like:

class myVector
{
public:
int size() const;
};

This tells the compiler that size() doesn't change the object.
It is true that I would like change the original object by this method.

Are you really sure about this? A copy constructor should usually not modify
the original object. Only in very rare cases, it's ok. Why do you think you
need it?
But this should be no mistake as such shouldn't it?

Well, it is possible, if you can live with the consequences, like that you
can't copy constant objects or temporaries, and that you can't use your
class with any standard container.
Strings can do exactly the same.

Well, they could, but they don't.
Thus, what I need should also be implemented in the std::string class
because you can say:
string s1("ladida");
string s2("testing"); and then
s1 = s1 + s2;

In this code, there is no need for a copy constructor to modify the original
object. I guess you are referring to the third line.

s1 = s1 + s2;

What happens here that s1.operator+(s2) is called. That one returns a new
string that contains the result of the concatenation. That string is then
copied on return into a temporary, which is then copied using operator=
into s1. Btw: it would be better to use += here, which does modify its left
hand operand instead of returning a new object.
 
S

silversurfer2025

OK, it all works now,. I had to declare the functions as const, which I
had forgotten before.. It is clear that a const object should not
invoke methods which are not const..

Greetings and thanks once more
Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top