Thread: help with overloaded operators

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    5

    help with overloaded operators

    i am writing a program that has to manipulate sparse polynomials, i have written all the linked list part of the program, but i am having troubles with writing some of the overloaded operators of (+) and of (-), any advice or help would be greatly appreciated, thanks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    If sp is a sparce polynomial.

    class sp
    {

    const sp& +operator( const sp& rsp )
    {
    // whatever you do to add sparse polynomials

    return m1 + rsp.m1; //m1 (member one) plus the reference m1 ( member one) or whatever is done to add a sparce polynomial
    }

    sp sp1, sp2, sp3;

    sp3 = sp2 + sp1; // sp2 would be the object calling the + operator and sp1 would be passed as the reference rsp

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    43

    Almost right :)

    Yes, but:
    1. Syntaxis of operator + is different ( '+' is at the end)
    2. You may have problems returning a reference, as its object will be destroyed at the end of sp:perator+, the inline function may help, but do not count on that. Returning a real object & propper copy constructor is better, I think

    class sp
    {
    sp(const sp&)
    {
    m1 = sp.m1; //or s'th like that
    }

    const sp operator+( const sp& rsp )
    {
    // whatever you do to add sparse polynomials

    return sp(m1 + rsp.m1); //m1 (member one) plus the reference m1 ( member one) or whatever is done to add a sparce polynomial
    }

    sp sp1, sp2, sp3;

    sp3 = sp2 + sp1; // sp2 would be the object calling the + operator and sp1 would be passed as the reference rsp

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    43

    Copy constructor

    Sorry:

    sp(const sp& other)
    {
    m1 = other.m1;
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    if you return an object adn not a reference, it doesn't have to be const.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    Oh, yes, I just copied your code and get rid of reference. Later I saw that, but being const is not a problem, even is better, but operator + should be const I think:

    const cp operator + (const cp& other) const
    {
    ....
    }

    or

    cp operator + (const cp& other) const
    {
    ....
    }

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    My bad. You're right on all counts. The return should be an object not a reference since operations creates a new object and it should be const since no operations should occur on what we've created, since the + operator does not create an l-value.
    Last edited by Dang; 10-17-2001 at 11:48 AM.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    why can the returned object be an l-value?
    suppose the class has a non-const method that evaluated a value and output it, why shouldn't we allow a syntax like
    (sp1+sp2).EvalAndOutput() ?

    since we create a temporary object, this object need not be const, and users of that object can use it anyway they like, including assigning to it, but that seems useles the copy constructor has some meaningful job.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    All of the temporary objects are const according to ANSI C++ standard, so this would violate the stanadard, even if possible

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    Do you have any proof that "temporary objects are const according to ANSI C++ standard"?

    Anyone lese have heard about it?
    I can't see any reason why it should be that way.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    Yes: "Thinking in C++, 2nd ed. Volume 1" by Bruce Eckel, see section: Operator Overloading, '+' operator; see also temporary objects
    Now a little example:

    struct TheClass
    {
    ....
    TheClass(int i);
    };

    void Func(TheClass& );

    void main()
    {
    int i = 5;
    Func(i); //The temporary object which is created here is const
    //but the function takes non-const reference
    //Some compilers generate warning, some do not
    //accept it
    }

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    well, I don't have the official ANSI standard, but from what I found in the net and my books about it, I think Eckel is wrong by saying (in page 284, first edition) that temporary objects are automatically const. I haven't seen any support for this.
    As Scott Meyers explains in More Effective C++ (Item 19) there are tow main reasons fortemporary objects - when a type conversion occures to make function calls succeed, and when functions returns objects.
    As mentioned in this item, there is a rule in the language that says that temporary objects cannot be assigned to a non-const references. As Meyers explains, this is because this probably not what the programmer wanted, because non-const references are meant to be changed, and by changing the reference, only the temporary object will be changed, and not the real data it was converted was.
    This is why your example fails to compile in VC++ with the following error:
    A reference that is not to 'const' cannot be bound to a non-lvalue

    But that doesn't say that temporary objects are const. You can still call non-const methods on them.

    Meyers do say (in Item 21 in the same book) that operator+() and operator*() should return a const object, because we don't want that objct to be used as l-value. It conforms to the general rule of "Do as the Ints do", meaning that your objects should behave similiar to the build-in types, and as they don't allow:
    (3*4) = 5
    so should your own object.

    But, in two STL implementation I looked into (VC++ & ObjectSpace), the operator& of string (actually basic_string) returns a string, not a const string. (again, I don't know what the standard says about it, but I gues they both agree with it).

    Conclusions?
    I think temporary objects are not const (unless declared const). Generally, I tend to agree with Meyers, but here, errrr, I don't know.
    The risk of someone using your object + return value as an l value:
    (x1 + x2) = x3
    seems unreal to me.
    But the benefits can be substantial, if someone wants to call a non-const method on the result:
    (x1+x2).Add(y).Print();

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    I am glad to see such responce in that forum.

    Your answer is almost perfect.
    As you, I haven't read the standard too.

    But I am sure standard doesn't tell you the way you should declare the '+' operator.

    Using VC++ for example is not a good idea: it is full of ANSI violations... But it also didn't compile the code:

    As you see in my code 'i' is implicitly casted to 'TheClass': a temporary object of type struct TheClass is created. So if this object wasn't a const the file would be compiled well.

    Lets suppose that code:

    struct TheClass
    {
    int n;
    TheClass(int i) : n(i){}
    };

    TheClass operator +(const TheClass& i, const TheClass& j)
    {
    return TheClass(i.n + j.n);
    }

    void Func(TheClass& );

    void main()
    {
    int i = 5;
    TheClass c1(i), c2(i);
    Func(c1 + c2);
    }


    The code is not compiled by GNU C++, for that reason. (it is compiled by MSVC++ 6.0 & gives a warning in Borland C++ by the way... )

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    well, you do have a point ergarding why it is good to return a const object, but regarding the standard, we both don't know it, so I'm not sure GNU ++ is better then VC++.
    According to VC++ message and to Meyers word, the standard disallows implicit conversion when the created temporary object is assigned to a non-const reference. That doesn't mean that all temporary object are const.

    BTW,
    If you used Func(TheClass(5)); - would GNU C++ eat it?


    Another BTW,
    The standard doesn't tell you the way you should declare THE '+' operator, but the sandard do have the STL, right? and it it has the declaration of the STL classes, right? So how is basic_string:perator+() defined there? form the two implementation I saw, it declared as returning a non-const basic_string.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    Oh, yes STL is a standard....
    Put on VC++ warings to 4-th level & try to compile program using it, the result is a flow of warings. If this is a standard...

    So we both have the conclusion that it is possible to return a non-const value, but people rarely need it (unless you compile with GNU C++).

    By the way you can try g++ under Windows environment as I do:
    http://sources.redhat.com/cygwin/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iostream overloaded operators
    By peckitt99 in forum C++ Programming
    Replies: 1
    Last Post: 08-10-2007, 05:32 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 05:07 AM
  4. Polymorphism & Overloaded Operators :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:40 PM
  5. overloaded operators
    By ivandn in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2001, 03:52 PM