Friends and Members

This is a discussion on Friends and Members within the C++ Programming forums, part of the General Programming Boards category; Ok, so I googled to see if I can see what the difference is between friends and members of a ...

  1. #1
    Village id10t
    Join Date
    May 2008
    Posts
    57

    Friends and Members

    Ok, so I googled to see if I can see what the difference is between friends and members of a class, but all sites seem a little hazy....

    I think a friend has all the power of a member, so why delcare it as a friend and not as a member? and what is the difference in declaring a friend from a member?

    Thanks!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    Friends and members are not the same thing.
    You can declare a class or a function (B) or a function (B) or another class (B) a friend of one class (A), in which case that function (B), class (B) or class member (B) will have full access to the class (A), including privately declared data.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Village id10t
    Join Date
    May 2008
    Posts
    57
    ok... I think I follow. what is the difference between the declaration statements of a friend and of a member...

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    Well, as I mentioned, they're not the same.

    Code:
    class foo
    {
        friend class foo2; // Friend declaration
        static void foo(); // Member declaration
    };
    
    class foo2
    {
    public:
        void foo() { foo::foo(); } // Legal since foo2 is a friend of foo.
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Village id10t
    Join Date
    May 2008
    Posts
    57
    ok- lets say I want to write the following friend statement as a member function instead

    Code:
    friend Pairs operator *(const Pairs& f, const Pairs& s);
    will it be something like
    Code:
    void Pairs operator*();
    Im still really confused about friends and members

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    No, the friend keyword is a keyword applied to a declaration in cases of functions.
    Define your function as
    Code:
    Pairs operator *(const Pairs& f, const Pairs& s);
    And inside the class, add the friend declaration:
    Code:
    friend Pairs operator *(const Pairs& f, const Pairs& s);
    The operator * will now have access to the class internals.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    A member is (as it says) part of the class. A friend is not part of the class, it belongs to something else entirely (or nothing at all), but the class has granted it access to the data.

    Your operator * will still return a Pairs element (I'm assuming -- it did before), it certainly won't return void. And you will still need to specify the right hand side of * -- you have to multiply your object by something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. namespaces and static class members
    By ygfperson in forum C++ Programming
    Replies: 1
    Last Post: 07-06-2003, 09:55 PM
  2. Protected Inheritance
    By golfinguy4 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2002, 10:56 AM

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