mainもフレンド関数にできるのかー。
#include <iostream> using namespace std; int main(); class Foo { private: int i; public: Foo(); Foo(int i); Foo &operator=(Foo &foo); friend int main(); }; Foo::Foo() : i(0) {} Foo::Foo(int i) : i(i) {} Foo &Foo::operator=(Foo &foo) { cout << "operator=:\t" << this << "<-" << &foo << "\n"; i = foo.i; return *this; } int main() { Foo foo1, foo2, foo3(100); cout << "&foo1:\t\t" << &foo1 << "\n"; cout << "&foo2:\t\t" << &foo2 << "\n"; cout << "&foo3:\t\t" << &foo3 << "\n"; foo1 = foo2 = foo3; cout << "foo1:\t\t" << foo1.i << "\n"; cout << "foo2:\t\t" << foo2.i << "\n"; cout << "foo3:\t\t" << foo3.i << "\n"; return 0; }
&foo1: 0x22cce4
&foo2: 0x22cce0
&foo3: 0x22ccdc
operator=: 0x22cce0<-0x22ccdc
operator=: 0x22cce4<-0x22cce0
foo1: 100
foo2: 100
foo3: 100