overloading +, * and = for complex objects

Discussion in 'C++' started by silversurfer2025, Jul 1, 2006.

  1. 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
     
    silversurfer2025, Jul 1, 2006
    #1
    1. Advertising

  2. silversurfer2025

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Jul 1, 2006
    #2
    1. Advertising

  3. 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.
     
    silversurfer2025, Jul 1, 2006
    #3
  4. 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;
    }


    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.
     
    silversurfer2025, Jul 1, 2006
    #4
  5. silversurfer2025

    Rolf Magnus Guest

    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 :)
     
    Rolf Magnus, Jul 1, 2006
    #5
  6. 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 :)
     
    silversurfer2025, Jul 1, 2006
    #6
  7. silversurfer2025

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Jul 1, 2006
    #7
  8. 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.
     
    silversurfer2025, Jul 3, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    605
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    797
    Stanimir Stamenkov
    Oct 25, 2005
  3. Pmb
    Replies:
    37
    Views:
    1,070
    tom_usenet
    Jun 3, 2004
  4. Robert Mark Bram
    Replies:
    0
    Views:
    718
    Robert Mark Bram
    Feb 4, 2007
  5. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    803
Loading...

Share This Page