CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Friend function

  1. #1
    Join Date
    May 2007
    Posts
    85

    Friend function

    Hello,
    Consider this scenario,
    Code:
    class B;
    class A
    {
        private:
              int x, y;
        public:
              A(){}
        friend void func( A a, B b );
    };
    
    class B
    {
        private:
              int i, j;
        public:
              B(){}
        friend void func( A a, B b );
    };
    Friend function is used to allow non-members to access private data defined inside a class. Now my question is I have two variables x and y in class A, and two variables i and j in class B. Is it possible that I can allow my users (outside class) to access only variable x and not y defined in class A ? Similarly, allow access outside class only to variable i and not to j in class B.
    NOTE: The above scenario should happen only when friend is defined.

    Thanks
    Last edited by Jacko123; August 20th, 2007 at 05:01 AM.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Friend function

    No, friend declaration makes both private and protected members accessible.

  3. #3
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: Friend function

    Directly it's not possible but you might put those variables which you don't want friend function to access in separate base classes holding that variable. Not sure on this. Maybe someone will correct/refine this.
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Friend function

    At the first seconds I had a similar thought but since I guess that inherited class need access in some way to base class members the friend function will also have access. Maybe there is a scenario where the method can be used however. Can't think of any at the moment so I join you sunnypalsingh in the 'not sure crowd'...

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Friend function

    Here is an example, how it could be done (even though this is a bit clumsy):
    Code:
    class A
    {
    private:
      int x, y;
    public:
      A(){}
      class XAccessor {
        XAccessor( A& a ) : x( a.x ) {}
        friend void func( A& a );
      private:
        int& x;
      };
    };
    
    void func( A& a ) {
      A::XAccessor(a).x = 0;
      a.y = 0;  // not allowed
      A::XAccessor(a).y = 0; // not possible
    }
    
    void otherfunc( A& a ) {
      A::XAccessor(a).x = 1; // not allowed
    }
    Last edited by treuss; August 20th, 2007 at 06:39 AM.

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Friend function

    Another way is to make y more private thus:
    Code:
    class B;
    class A
    {
    private:
       int x;
    
       class very_private
       {
              int y;
              friend class A;
       } y_access;
    
       friend void func( A a, B b );
    };
    func can access a.x but cannot access a.y_access.y because friendship isn't transitive.

  7. #7
    Join Date
    Nov 2006
    Posts
    42

    Re: Friend function

    Quote Originally Posted by NMTop40
    Another way is to make y more private thus:
    [code]


    class very_private
    {
    int y;
    friend class A;
    } y_access;
    sorry for interrupting but
    y_access followed by class very_private braces then ";"

    what kind of a syntax is this? means
    am i wrong ? or you ?
    Last edited by daemonakadevil; August 20th, 2007 at 08:21 AM.

  8. #8
    Join Date
    Feb 2007
    Posts
    141

    Re: Friend function

    Quote Originally Posted by daemonakadevil
    sorry for interrupting but
    y_access followed by class very_private braces then ";"

    what kind of a syntax is this? means
    am i wrong ? or you ?

    Brother he is creating object of class very_private nothing more then that.

  9. #9
    Join Date
    Feb 2007
    Posts
    141

    Re: Friend function

    Another solution, not best but ya one of the possible solution

    Code:
    #include <iostream>
    class B;
    class tempA
    {
        private:
            int x;
        public:
            tempA(){x=0;}
    };
    class A : private tempA
    {
        private:
              int y;
        public:
              A(){y=0;}
        friend void func( A a, B b );
    };
    class tempB
    {
        private:
            int i;
        public:
            tempB(){i=0;}
    };
    class B:private tempB
    {
        private:
              int j;
        public:
              B(){j=0;}
        friend void func( A a, B b );
    };
    
    void func(A a, B b)
    {
        std::cout<<"A.y = "<<a.y<<" , B.j = "<<b.j<<std::endl;
        //std::cout<<"A.x = "<<a.x<<" , B.j = "<<b.i<<std::endl;
        // can't use this statement because now x and i are private members
    }
    int main()
    {
    	A a;
    	B b;
    	func(a,b);
    	return 0;
    }

  10. #10
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Friend function

    The problem with your solution is that x is private to A as well, and i is private to B, i.e. A cannot access x and B cannot access i.

  11. #11
    Join Date
    Feb 2007
    Posts
    141

    Re: Friend function

    Quote Originally Posted by NMTop40
    The problem with your solution is that x is private to A as well, and i is private to B, i.e. A cannot access x and B cannot access i.
    Yes you correctly identified problem in my code. Thanks for that.
    I have to use tempA as friend class in class A. and same for tempB also

    Thanks for correcting my code.

  12. #12
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  13. #13
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Friend function

    Quote Originally Posted by exterminator
    Sounds like a flawed design.
    Doesn't that apply to any use of friend?

  14. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Friend function

    Doesn't that apply to any use of friend?
    Not necessarily. For example, overloading operator<< and operator>> for I/O streams is valid use of friend functions. Consider the arguments given by Does "friend" violate encapsulation?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  15. #15
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Friend function

    Quote Originally Posted by treuss
    Doesn't that apply to any use of friend?
    How? Ask a question, why is that the friendship in the OP's case is causing you to "split" your original class into an internal class with an explicit accessor or causing you to create extra types (that might not sometimes make sense as individual entities or abstractions) and "different" friendships? Does it really make sense to use friends when the friend should have access to just part of the private data and not all of those? Or better put it that you want to ensure/enforce that access to other private data of a friend needs to be protected? Why need to make such a distinction?

    To me it looks like:
    • original friendship does not make sense
    • the original class needs to be segregated into more granular distict types (in which case, your or NMTop40's solutions fit right)
    • the point to protect rest of the data is immaterial
    A class can have many members and considering operator>> overloads which can be friend, do you protect the data members that you don't want to publish via the operator to the streams?

    Re-reading the original post,
    Quote Originally Posted by Jacko123
    Is it possible that I can allow my users (outside class) to access only variable x and not y defined in class A ? Similarly, allow access outside class only to variable i and not to j in class B.
    I am guessing that it doesn't even concern a friend. The question is about users of the class and not friends. So, since the data members in A and B are private, nothing is accessible. For a few selected data members to be accessible the only thing needed is a modifier and an accessor (getter/setter). May be the orginal post is not clear on this point but my above arguments hold.
    Last edited by exterminator; August 21st, 2007 at 05:48 AM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured