Thread: Overloading ==

  1. #16
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Friend functions allow other classes access to private data members. This is usually looked down upon because it violates the object oriented principle of encapsulation.

  2. #17
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    while it is often true that the arithmetic operators are implemented as non-member functions i cannot ever recall making comparison functions non-members. You really should invest in a much better text or two if you are serious about learning c++.
    As a sidenote the reason that overloading >> and << for printing etc. is done as a non-member is because to be a member function they would have to be members of istream and ostream. This is obviously not possible. The most common implementation of them is just to forward a call to a member function that takes and returns a stream reference then no friendship need be granted either.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #18
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by Stoned_Coder
    while it is often true that the arithmetic operators are implemented as non-member functions i cannot ever recall making comparison functions non-members. You really should invest in a much better text or two if you are serious about learning c++.
    The textbook I have is what my programming class uses in school. Unfortunately, that class hasn't covered arrays, structs, or classes yet, so I'm on my own for now learning that stuff. I'm serious about learning to program, but I'm also seriously poor. I'm waiting for C++ for Dummies to be returned to the library, and some day when I have some time, I'd like to try to hunt down a free e-book. I know there's probably plenty of good links in past threads on this board. I just need to take the time to go looks them up. I guess I ought to just search around on my own to figure out this whole friend deal. Anyway, thanks for your help.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #19
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

  5. #20
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Exactly as PJYelton, and stoned_coder said...
    You should always keep this in mind: Whenever you can use a member function, use it, it's always better than firend functions.
    none...

  6. #21
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by ammar
    Exactly as PJYelton, and stoned_coder said...
    You should always keep this in mind: Whenever you can use a member function, use it, it's always better than firend functions.
    I'm sure that'd be great advice if only I knew what a friend function is. When I have time I'll take another look at "Thinking in C++" to see if friend functions are covered in there. Thanks for all of the help, everybody
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  7. #22
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    A friend function is a function that has the right to access private and protected data members of the class it's has been declared as it's friend, for example:
    Code:
    class myClass{
    friend void friendFunction(myClass &);
    public:
    //some members
    private:
    //int x;
    };
    
    void friendFunction(myClass &obj)
    {
         obj.x = 5;     //because friendFunction is a friend of myClass
                             //it can access the private member x of class 
                             //myClass
    }
    Hope friend functions are clear now, if they are not feel free to ask.
    none...

  8. #23
    Registered User
    Join Date
    Dec 2002
    Posts
    29
    "Whenever you can make a function a member instead of a friend, you should"

    This is not true for operator functions. Some are better implemented as non-member functions. You can usually do this without making them a friend, although sometimes making them a friend is an option. There are tradeoffs you have to evaluate. The main reason you don't make some operator functions member functions is to make them operate like built in types.

    Get the two Effective C++ books by Scott Meyers. You shouldn't be without them.

  9. #24
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Actually using a friend function makes sense, sometimes. For instance, if you are not comparing the same class type, ie. if it makes sense to compare MyClass to an int, you will want to define operator== two different ways:


    Code:
    class MyClass {
    int somedata;
    public:
    MyClass(int i) { somedata = i; }
    
    // some implementation goes here ...
    
    // comparison operators:
    bool MyClass::operator==(const int& i) const
    {
        if ( somedata == i ) return true;
        else return false;
    }
    
    friend operator==(const int& i, const MyClass& mc)
    {
        if ( mc.somedata == i ) return true;
        else return false;
    }
    };
    
    // Now the user of the class can put the object either on the 
    // rhs or lhs of the operator==:
    
    int main()
    {
        MyClass mc(34);
    
        if ( mc == 34 ) {// do something
        }
    
        if ( 34 == mc ) { // this works too
        }
    
        return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  10. #25
    Registered User
    Join Date
    Dec 2002
    Posts
    29
    You can get that same functionality by using one == function and some form of implicit or explicit conversion too. Also, you can avoid making the function a friend by using an accessor function.
    Last edited by dynamic_cast; 01-11-2003 at 01:05 AM.

  11. #26
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Hope this helps :)

    #include <iostream>

    class Y
    {
    int i;
    char ch;
    public:
    bool operator==(const Y &);
    friend bool operator ==(const int &, const Y &);
    };

    bool Y:perator==(const Y &ob)
    {
    if(i == ob.i && ch == ob.ch)
    return true;
    else
    return false;
    }

    bool operator==(const int &val, const Y &ob)
    {
    if(val == ob.i)
    return true;
    else
    return false;
    }

    int main()
    {
    Y ob1, ob2;
    int x = 20;

    if(ob1 == ob2)
    std::cout << "the two objects are equal .. that is "
    "the datamembers of both the objects"
    "hold similar values"
    << std::endl;

    if(x == ob1)
    std::cout << "the integer x is equal to the value"
    " of the datamember i of the object ob1"
    << std::endl;

    // imagine ob1 == ob2 this way.... ob1.==(ob2);
    // in the sence an object can only call its member functions

    // but x == ob1 cannot be looked in similar fashion since
    // == is not a member function of int that can take an object
    // of type class Y in the RHS
    // in such situations where your LHS is not an object of the class
    // whose object appears on the RHS
    // you would have to take help of friend functions

    // please note there are certain restrictions w.r.t operators
    // that could be made as friends
    // my aim here is just to help you give an idea as to when
    // might need friend functions
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  12. #27
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by dynamic_cast
    "Whenever you can make a function a member instead of a friend, you should"

    This is not true for operator functions. Some are better implemented as non-member functions. You can usually do this without making them a friend, although sometimes making them a friend is an option. There are tradeoffs you have to evaluate. The main reason you don't make some operator functions member functions is to make them operate like built in types.

    Get the two Effective C++ books by Scott Meyers. You shouldn't be without them.
    Excuse me...
    You are saying that: "This is not true for operator functions. Some are better implemented as non-member functions. You can usually do this without making them a friend,"...
    That doesn't mean that: "Whenever you can make a function a member instead of a friend, you should"" is not true... The thing you are talking about is not realated to what I was talking about...

    IfYouSaySo, ofcourse impelenting a function as a friend makes sence sometimes, but you should keep in mind that if you can implement it as a member function you should do so.
    none...

  13. #28
    Registered User
    Join Date
    Dec 2002
    Posts
    29
    Originally posted by ammar

    IfYouSaySo, ofcourse impelenting a function as a friend makes sence sometimes, but you should keep in mind that if you can implement it as a member function you should do so.
    Thats still wrong. There are many cases when you can but shouldn't.

  14. #29
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by dynamic_cast
    Thats still wrong. There are many cases when you can but shouldn't.
    Maybe I'm wrong, but can you give me some examples, were I can use a member function and it's better to use a friend function.
    none...

  15. #30
    Registered User
    Join Date
    Dec 2002
    Posts
    29
    Yeah sure. Ummm...the two off the top of my head are operator+ and operator<< and >>.

    Both can be implemented as member functions. But it's better to implement opertaor+ as a toplevel function so that you can say '2 + class' as well as 'class + 2'. A memeber function would usually be able to do 'class + 2' by converting 2 to a class object implicity through the constructor or explicitly through an operator function. It won't be able to do '2 + class' though. ( 2.operator+(class) ) It won't convert the 2 in this case. Which brings up a good point. Anytime you want a conversation on both the left and right parameters you need a top level function. You could make it a member function...but it just wouldn't be a very good design.

    operator>> and << need it because of convention. You could make them member functions, but then you'd have to write class >> cout and class << cin. Or youd have to overload the i and ostream class. Both bad ideas. So you use a top level function.

    Other things prob come up to...but the point is, you can use a member function in every case here...but it's better to use a toplevel function. I didn't say a friend function...you can always use accessor memeber functions to avoid using a friend.

    Sorry bout that rambled mess...it's 4 a.m. and I'm tired as hell.

    edit:
    O yeah...I wasn't really complaining about the friend part...but the statement is still untrue...even if you had to use a friend it would be better to in these exaples than using a memeber function. Fortuantley you don't have to use a friend because of accessor functions...though sometimes making them friends can have its benefits.
    Last edited by dynamic_cast; 01-11-2003 at 03:04 AM.

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, 02: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