标签:
//OpeClass.h #pragma once class OpeClass { friend int func(const OpeClass xx); public: OpeClass(void); OpeClass(int x,int y); ~OpeClass(void); private: int width; int height; };
//OpeClass.cpp #include "OpeClass.h" OpeClass::OpeClass(void) { width = 50; height = 50; } OpeClass::OpeClass(int x,int y):width(x),height(y) { } OpeClass::~OpeClass(void) { } int func(const OpeClass xx) { return xx.height * xx.width; }
//main.cpp #include "OpeClass.h" #include <iostream> using namespace std; void main() { OpeClass XO; cout<<func(XO); system("pause"); }
//A.h #pragma once #include <iostream> using namespace std; class A { friend class B; public: ~A(void); static void func() { cout<<"This is in A"<<endl; } private: A(){}; static const A Test; };
//A.cpp #include "A.h" const A A::Test = A(); A::~A(void) { }
//B.h #pragma once #include "C.h" class B { public: B(void); ~B(void); void func(C& c); };
//B.cpp #include "B.h" #include "A.h" #include "C.h" #include <iostream> using namespace std; B::B(void) { } B::~B(void) { } void B::func(C& c) { cout<<"This is in B"<<endl; A::Test.func(); c.func(A::Test); }
//C.h #pragma once class A; class C { public: C(void); ~C(void); void func(const A& a); };
//C.cpp #include "C.h" #include <iostream> using namespace std; C::C(void) { } C::~C(void) { } void C::func(const A& a) { cout<<"This is in C"<<endl; }
//main.cpp #include "A.h" #include "B.h" #include "C.h" #include <iostream> using namespace std; void main() { B b; C c; b.func(c); system("pause"); }
这个略微有点复杂,由于你要类成员函数作为友元,你在声明友元的时候要用类限定符,所以必须先定义包括友元函数的类,可是在定义友元的函数时候。又必须事先定义原始类。通常的做法先定义包括友元函数的类。再定义原始类。这个顺序不能乱。(假设是友元类,则没有这样的这样的必须)如以下所看到的:
//B.h #pragma once class A; class B { public: B(void); ~B(void); int func(A xx); };
//A.h #pragma once #include "B.h" class A { friend int B::func(A xx); public: A(void):mx(20),my(30){} ~A(void){} private: int mx; int my; };
//B.cpp #include "B.h" #include "A.h" B::B(void) { } B::~B(void) { } int B::func(A xx) { return xx.mx * xx.my; }
//main.cpp #include "A.h" #include "B.h" #include <iostream> using namespace std; void main() { A a; B b; cout<<b.func(a)<<endl; system("pause"); }
//A.h #pragma once class A { friend class B; public: A(void); ~A(void); int funa(B& b); private: int mx; int my; };
//A.cpp #include "A.h" #include "B.h" A::A(void) { mx = 10; my = 10; } A::~A(void) { } int A::funa(B& b) { return b.mb * b.mc; }
//B.h #pragma once class B { friend class A; public: B(void); ~B(void); int funb(A& a); private: int mb; int mc; };
//B.cpp #include "B.h" #include "A.h" B::B(void) { mb = 20; mc = 20; } B::~B(void) { } int B::funb(A& a) { return a.mx *a.my; }
//main.cpp #include "A.h" #include "B.h" #include <iostream> using namespace std; void main() { A a; B b; cout<<a.funa(b)<<endl; cout<<b.funb(a)<<endl; system("pause"); }
//A.h #pragma once // class B is a friend class of A class A { friend class B; public: A(void):ma(10),mb(20){} ~A(void){} int funa(B& b); private: int ma; int mb; };
//B.h #pragma once #include "A.h" // A's function funa is a friend function of B class B { friend int A::funa(B& b); public: B(void); ~B(void); int funb(A& a); int func(A& a); private: int mx; int my; };
//A.cpp #include "A.h" #include "B.h" int A::funa(B& b) { return b.mx * b.my; }
//B.cpp #include "B.h" B::B(void):mx(12),my(15) { } B::~B(void) { } int B::funb(A& a) { return a.ma + a.mb; } int B::func(A& a) { return a.ma * a.mb; }
//main.cpp #include "A.h" #include "B.h" #include <iostream> using namespace std; void main() { A a; B b; cout<<a.funa(b)<<endl; cout<<b.funb(a)<<endl; cout<<b.func(a)<<endl; }
版权声明:本文博主原创文章。博客,未经同意不得转载。
标签:
原文地址:http://www.cnblogs.com/mengfanrong/p/4914171.html