Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overloading +, * and = for complex objects

Reply
Thread Tools

overloading +, * and = for complex objects

 
 
silversurfer2025
Guest
Posts: n/a
 
      07-01-2006
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

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      07-01-2006
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.

 
Reply With Quote
 
 
 
 
silversurfer2025
Guest
Posts: n/a
 
      07-01-2006
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.


 
Reply With Quote
 
silversurfer2025
Guest
Posts: n/a
 
      07-01-2006
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[i] = pFeatVec[i] + val[i]; is working properly,
it copies the elements and
// adds them into the local variable.

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


silversurfer2025 schrieb:

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


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-01-2006
silversurfer2025 wrote:

> 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&)
>
>> What's that supposed to mean?


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&)

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


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

 
Reply With Quote
 
silversurfer2025
Guest
Posts: n/a
 
      07-01-2006
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

Rolf Magnus schrieb:

> silversurfer2025 wrote:
>
> > 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&)
> >
> >> What's that supposed to mean?

>
> 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&)
>
> >> > 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.

>
> It's just the other way round, but you already know that now


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-01-2006
silversurfer2025 wrote:

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

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


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.

 
Reply With Quote
 
silversurfer2025
Guest
Posts: n/a
 
      07-03-2006
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

Rolf Magnus schrieb:

> silversurfer2025 wrote:
>
> > 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.
>
> >> 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.

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How complex is complex? Kottiyath Python 22 03-28-2009 10:11 PM
wsdl2java: method parameter a complex type that extends another complex type Robert Mark Bram Java 0 02-04-2007 10:06 AM
[XML Schema] Content type of complex type definition with complex content Stanimir Stamenkov XML 2 10-25-2005 10:16 AM
Overloading the "=" operator for Complex numbers Pmb C++ 37 06-03-2004 09:25 AM
For expert on complex loops (reposted) - complex looping problem news.amnet.net.au Java 1 04-13-2004 07:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57