Thursday, November 01, 2007

Friend function example

// friend_functions.cpp
// compile with: /EHsc
// http://msdn2.microsoft.com/en-us/library/h2x4fzdz(VS.80).aspx
#include

using namespace std;
class Point
{
friend void ChangePrivate( Point & );
public:
Point( void ) : m_i(0) {}
void PrintPrivate( void ){cout << m_i << endl; }

private:
int m_i;
};

void ChangePrivate ( Point &i ) { i.m_i++; }

int main()
{
Point sPoint;
sPoint.PrintPrivate();
ChangePrivate(sPoint);
sPoint.PrintPrivate();
}

No comments: