Overloading ==

This is a discussion on Overloading == within the C++ Programming forums, part of the General Programming Boards category; Well, I hope you have some sleep now, I know how does it feel, when it's 4 am! In the ...

  1. #31
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Well, I hope you have some sleep now, I know how does it feel, when it's 4 am!

    In the example you gave about overloading the operator << and operator >> you cann't make them member functions because they should be members in the cout and cin objects, and that's not possible, and the only way is to make them friend functions...

    And for the example: object + 2 and 2 + object, If you want to implement a them both as friend function you will have to write two functions, but would do it, by making the function 2 + object a friend function and the object + 2 function a member function...

    About the statement I gave, I think it's a general programming principle...

    But anyways I might be wrong, because I'm not experienced in C++...
    Maybe we can make a thread about that, and ask for the advice of experiences programmers...
    none...

  2. #32
    Registered User
    Join Date
    Dec 2002
    Posts
    29
    [i]

    In the example you gave about overloading the operator << and operator >> you cann't make them member functions because they should be members in the cout and cin objects, and that's not possible, and the only way is to make them friend functions...

    [i]
    You can make them members.
    But you'd have to use the syntax 'class >> cout'
    which is fine except it's not the same as how built in types work, which is confusing.

    And no...you don't have to make two functions. You would most likeley take advantage of the implicit conversion of a constructor...or you could make an explicit conversion function. You don't NEED to make operator+ a friend, but maing it a member function is def the wrong way to go. It should always be a top level function.

  3. #33
    Registered User
    Join Date
    Jan 2003
    Posts
    12
    just because of the class + 2, 2+ class issue, i'd make operator+ a friend function ..

    And for the example: object + 2 and 2 + object, If you want to implement a them both as friend function you will have to write two functions, but would do it, by making the function 2 + object a friend function and the object + 2 function a member function...
    I don't think you need 2 functions for it ?

    friend Class operator*(double, const Class &)

    allows me to write (when double d, class c)

    x= C * d as well as x = d *C ...
    since the compiler sees it as
    x=operator*(d, c) ..

    which is the only use for friend-functions and the reason of their existence ...

    a normal member function would be translated as
    d *c
    x=d.operator*(c) , making operator* a memberfunction of double which insn't possible and against the rules of OO

    Correct me if I'm wrong but I don't see a way to be able to use associativity without friend functions

  4. #34
    Registered User
    Join Date
    Dec 2002
    Posts
    29
    You can use accessor functions so that you don't need to make it a friend.

  5. #35
    Registered User
    Join Date
    Jan 2003
    Posts
    12
    http://www.cs.rit.edu/~icss263/writing.classes/p8.html

    If my quick read it correct accessor = getter/setter functions ?

    Then please give me an example how to use it for c * d and d * c, because I would like to know.

    edit : and why should I use accessor functions in stead of friend functions ?

  6. #36
    Registered User
    Join Date
    Dec 2002
    Posts
    29
    Yes, it's getters and setters. You should use them instead of a friend function because people are anal and will ***** at you if you use friend functions...seriously, friend functions violate information hiding and the whole OOP thing (at least most say so), so using getters and setters is a better idea...sometimes a friend function would simplify things though.
    Code:
    class rational
    {
    public:
       rational(int num); //just initialize num
       rational(int num, int denom); //initialize num and denom
    blah blah blah...
    };
    Okay, so you have a fractions function that can be intialized with a numerator and denominator or with just a numerator (you would make the denom default to 1 in the implementation).

    now you have a top-level function that takes two rationals.

    const rational operator+(rational first, rational second) //my best guess at this

    Now you do '2 + rational' of course the scond rational works, but the first is an int...so your screwed...no, wait...the compiler knows if it supplies an int to the rational constructor it can make a rational. So it does and your all set. The same works for 'rational + 2'. You could also make a conversion operator function.

    The compiler will only do conversions on both args if it's a top level function.

    Anyway...sorry I'm not much of an explainer. And sorry if the code is screwed up. I don't really program. But hopefully you get the idea.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 PM
  3. Separate long string into multiple arrays
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-27-2007, 01:57 AM
  4. One quick question...
    By Kross7 in forum C++ Programming
    Replies: 10
    Last Post: 04-13-2007, 09:50 PM
  5. Is there a bug in this part of my algorithm for connect 4?
    By Nutshell in forum Game Programming
    Replies: 8
    Last Post: 04-28-2002, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21