Thread: Why can friend operators be public or private?

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    Why can friend operators be public or private?

    EDIT: If I had more space for the thread title, it should've been entitled:

    "Why doesn't it matter if friend operators are public or private?"

    I know the friend keyword gives a class or function access to a classes private data, but i'm confused about how they behave with it's calling class.
    I mean, if a function/operator/class is in the private section, I shouldn't be allowed to access it outside the class right?


    Given the following example:
    Code:
    class Test{
    	
    	public:
    	
    		friend bool operator==( const Test& lhs, const Test& rhs ){
    			
    			return lhs.data == rhs.data;
    		};
    		
    		friend bool operator!=( const Test& lhs, const Test& rhs ){
    			
    			return lhs.data != rhs.data;
    		};
    		
    	public:
    	
    		Test( int d ) : data( d ) {};
    		
    	private:
    	
    		int data;
    };
    
    int main(){
    	
    	Test a( 1 );
    	
    	Test b( 2 );
    	
    	if( a != b ){
    		
    		std::cout << "A and B are NOT equal.\n";
    	}
    	
    	return 0;
    }
    It makes no difference whether or not the overloaded operators are in the public or private sections.

    1) Is that true? Or is there more testing that can prove otherwise?

    2) If it doesn't make a difference, why doesn't it make a difference?

    Thanks in advance
    Last edited by dudeomanodude; 04-17-2008 at 08:14 AM.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dudeomanodude View Post
    It makes no difference whether or not the overloaded operators are in the public or private sections.
    Of course not. A friend declaration tells the compiler to allow the named function or class full access to the class where it is declared friend. Whether this declaration is placed in a public, protected, or private area of the class definition is irrelevant. It is irrelevant because the only effect of the friend declaration is on the function or class named in that declaration. Nobody else is even aware that the friend exists.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Note that the friend functions are not member functions. You have only a declaration of functions that will be defined later and the friend keyword to indicate that they are friends. When declaring a friend function the function is not part of the class.

    It is only for member functions and variables that it matters whether they are public or private (or protected) because public/private governs access to members.

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Code:
    class Test{
    	
    	private:
    	
    		friend bool operator!=( const Test& lhs, const Test& rhs ){
    			
    			return lhs.data != rhs.data;
    		};
    		
    	public:
    	
    		Test( int d ) : data( d ) {};
    		
    	private:
    	
    		int data;
    };
    
    class TestChild : public Test{
    	
    	public:
    	
    		TestChild( int d ) : Test( d ) {};
    };
    
    class TestUnrelated{
    	
    	public:
    	
    		TestUnrelated( int d ) : m_Test( new Test( d ) ) {};
    		
            private:
    
    		friend bool operator!=( const TestUnrelated& lhs, const TestUnrelated& rhs ){
    		
    			return lhs.m_Test != rhs.m_Test;
    		};
    		
    	private:
    	
    		Test* m_Test;
    };
    
    int main(){
    	
    	TestChild a( 1 );
    	TestChild b( 2 );
    	
    	if( a != b )
    		std::cout << "A and B are NOT equal.\n";
    	
    	TestUnrelated c( 5 );
    	TestUnrelated d( 6 );
    	
    	if( c != d )
    		std::cout << "C and D are NOT equal.\n";
    	
    	return 0;
    }
    And it holds true for in all the cases I have above. The behavior is as I should've expected. Anyway thanks for your help on such a dumb question.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by dudeomanodude View Post
    [code]And it holds true for in all the cases I have above. The behavior is as I should've expected. Anyway thanks for your help on such a dumb question.
    It's not a dumb question. It's the kind of thing I've never seen asked here before and others may well read this thread and think "oh that's why".

    One thing to note though, you don't need a semi-colon after the closing brace of a function. You only need that after the closing brace of the class definition.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. webBrowser problem
    By algi in forum C# Programming
    Replies: 7
    Last Post: 08-19-2005, 09:35 AM
  2. I need a code for Tic Tac Toe game ?
    By martyr in forum C++ Programming
    Replies: 11
    Last Post: 12-07-2003, 03:29 AM
  3. VS C++ db with ado.net
    By student2005 in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2003, 02:46 PM
  4. C++ database
    By student2005 in forum C Programming
    Replies: 2
    Last Post: 11-27-2003, 11:37 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM