|
一、友元的概念: 1、什么是友元?
2、友元的用法:
3、友元的语法: 在类中使用friend 关键字对函数或者类进行声明: class Test8 j- A. b& N, z0 L+ t, u* H{2 ]! J K. V7 `, h* T double x;( i5 `* L" ~2 h9 d; \' Y3 T double y; friend void func(Test& t); ' T" k' a5 H: G$ L7 j }; void func(Test& t)) Q+ o. z0 m' i) T { ' }! X' }# c+ U$ j$ W. w 2 {' ? t$ B' [/ V: u } 注意:func() 全局函数是 Test 类的友元,func() 可以访问Test 类的所有成员,但是 func() 不是 Test 的成员函数。 示例代码: #include <stdio.h>#include <math.h> class Test { double x;) \7 M( o9 m( E. l4 G. ^" i double y; public: Test(double x,double y) {8 L6 _% E K% U2 \$ q$ w this->x=x; this->y=y; } double getX() { return x;. A) J j6 E- o4 y }! S# j2 Z* v# Y4 b% e0 U/ [ double getY() { ; ~- J& c* c5 S return y;8 s) y8 \5 n" {, X+ h } friend double func(Test& t1,Test& t2);3 I% H! ]% i+ Q' G };- o% {8 F' V1 S2 O- u2 Z ( @) ]! E5 R w$ E' W) M double func(Test& t1,Test& t2) { double ret =0; ret = (t2.y-t1.y)*(t2.y-t1.y)+(t2.x-t1.x)*(t2.x-t1.x);& U1 b) p. L7 q& i9 q$ `5 \ " `: t! t, L$ u3 I# A; y ret = sqrt(ret);9 ?! _9 y( t* S$ V' w ( `0 F! z* X8 ?' Z return ret; * T. U/ e+ g3 Z( F" e } int main() {* B! ^, t9 g# m1 M Test t1(1,2);, R# k/ N" N. ], t; C% g7 ? Test t2(10,20);) F# r9 M. S* K) Y; G$ R printf("t1(%f,%f)\n",t1.getX(),t1.getY()); printf("t2(%f,%f)\n",t2.getX(),t2.getY());" ?' t R: F; I6 i4 X$ V7 Z3 t! V printf("(t1,t2)=%f\n",func(t1,t2)); $ R3 ~8 {* C( w- u2 g return 0;# M9 C- h. F, | }! ^6 D4 P+ L. T0 L! e 输出结果: root@txp-virtual-machine:/home/txp/add# g++ test.cpproot@txp-virtual-machine:/home/txp/add# ./a.out t1(1.000000,2.000000)+ Y$ }, Y8 {7 A9 y, l t2(10.000000,20.000000) (t1,t2)=20.124612 4 H' e8 {, H/ i8 {9 \2 n6 N 4、友元的尴尬:
5、注意事项:
代码测试: #include <stdio.h>! W2 R& y# J7 E& ~ class ClassC- L' _: ^3 t% p; b' X0 k$ \ {$ l9 q4 N! Z. J9 F0 l9 F const char* n; public: ClassC(const char* n)7 S; L! R1 l3 e2 {; ]" p {% ?0 D- Z4 g2 Y7 O* |; P this->n = n;& H( t+ T; o5 K6 U4 |8 u2 N }7 c( I, [$ E9 m, Y& u friend class ClassB; };3 A% d' D3 M9 V( r) C0 r ( j$ m6 R% e7 ? e class ClassB( p+ P& g7 _# r& x: v7 E. q8 N! d {; m' M4 O* }# d, ] const char* n;! E' l& @4 G( P: i. S" D public:6 v1 [+ ~* f; t) Y8 ` ClassB(const char* n) {$ U& f8 y4 Z8 d$ ?1 p* X this->n = n;3 p, }$ F: x; X2 { } ) p0 _/ {( h1 s9 Q# N; \: y! h void getClassCName(ClassC& c) { printf("c.n = %s\n", c.n); }7 e) q- O2 Y' o0 G " f% G. w0 K2 Q" s4 n; } friend class ClassA; };& u) A+ Z% }' O* I& ? class ClassA { const char* n;, p. P4 ]4 K0 e% M! x8 m public: ClassA(const char* n)! w4 G8 ^ d! y: F& d% B { this->n = n;/ Z* B7 @0 V( h }) ]* s; [, o7 X* C void getClassBName(ClassB& b) {) x) m' b2 E" u, A+ c0 u# K3 } `3 h printf("b.n = %s\n", b.n); }& e7 Z/ K/ ^) O( G /* void getClassCName(ClassC& c) {; u0 h `2 j$ |: C) v( b0 e6 d: | printf("c.n = %s\n", c.n);6 [5 m, `$ M9 m& Y9 w } */( J. F1 L M; n9 @& `( g5 \ }; int main() { ClassA A("A");1 _* U: n/ P2 r& P$ I8 o ClassB B("B"); q E1 k+ y; U; l. Q+ t ClassC C("C"); 7 e; \' T+ H: s7 q0 G8 B4 k% J: | A.getClassBName(B);' x; O1 q( U2 z, ?* e B.getClassCName(C);$ F& @( H) f9 P7 Z3 v return 0; } : Z7 R3 m, U8 g$ F( S# v 输出结果: root@txp-virtual-machine:/home/txp/add# ./a.outb.n = B: y- {& w- j! ] c.n = C 如果把上面屏蔽的那部分代码打开,编译就会报错(因为友元没有传递性哦): root@txp-virtual-machine:/home/txp/add# g++ test.cpptest.cpp: In member function ‘void ClassA::getClassCName(ClassC&)’:( b! i+ u/ l5 Z; M9 _) W7 T test.cpp:5:17: error: ‘const char* ClassC::n’ is private const char* n; ^7 p+ [7 i1 i' B1 p3 O( h! K test.cpp:48:32: error: within this context4 A3 S* D0 Z$ P! t0 [$ A* q& } printf("c.n = %s\n", c.n);, P9 j2 d$ o! d l9 o) a ^ 2 g1 y& K- x' Z2 t2 b, \1 |2 F 6、小结:
' Q: _8 S- B. E- ?4 E 二、总结: 5 Y; {5 h' L# | _/ r 好了,今天的分享就到这里,如果文章中有错误或者不理解的地方,可以交流互动,一起进步。 2 t9 ~+ m; c; |7 w6 ~& n4 Z9 h |
微信公众号
手机版