
一、友元的概念: 1、什么是友元?
![]() 2、友元的用法:
3、友元的语法: 在类中使用friend 关键字对函数或者类进行声明: class Test{ double x; double y; friend void func(Test& t); }; void func(Test& t) { } 注意:func() 全局函数是 Test 类的友元,func() 可以访问Test 类的所有成员,但是 func() 不是 Test 的成员函数。 示例代码: #include <stdio.h>#include <math.h> class Test { double x; double y; public: Test(double x,double y) { this->x=x; this->y=y; } double getX() { return x; } double getY() { return y; } friend double func(Test& t1,Test& t2); }; 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); ret = sqrt(ret); return ret; } int main() { Test t1(1,2); Test t2(10,20); printf("t1(%f,%f)\n",t1.getX(),t1.getY()); printf("t2(%f,%f)\n",t2.getX(),t2.getY()); printf("(t1,t2)=%f\n",func(t1,t2)); return 0; } 输出结果: root@txp-virtual-machine:/home/txp/add# g++ test.cpproot@txp-virtual-machine:/home/txp/add# ./a.out t1(1.000000,2.000000) t2(10.000000,20.000000) (t1,t2)=20.124612 4、友元的尴尬:
5、注意事项:
![]() 代码测试: #include <stdio.h>class ClassC { const char* n; public: ClassC(const char* n) { this->n = n; } friend class ClassB; }; class ClassB { const char* n; public: ClassB(const char* n) { this->n = n; } void getClassCName(ClassC& c) { printf("c.n = %s\n", c.n); } friend class ClassA; }; class ClassA { const char* n; public: ClassA(const char* n) { this->n = n; } void getClassBName(ClassB& b) { printf("b.n = %s\n", b.n); } /* void getClassCName(ClassC& c) { printf("c.n = %s\n", c.n); } */ }; int main() { ClassA A("A"); ClassB B("B"); ClassC C("C"); A.getClassBName(B); B.getClassCName(C); return 0; } 输出结果: root@txp-virtual-machine:/home/txp/add# ./a.outb.n = B c.n = C 如果把上面屏蔽的那部分代码打开,编译就会报错(因为友元没有传递性哦): root@txp-virtual-machine:/home/txp/add# g++ test.cpptest.cpp: In member function ‘void ClassA::getClassCName(ClassC&)’: test.cpp:5:17: error: ‘const char* ClassC::n’ is private const char* n; ^ test.cpp:48:32: error: within this context printf("c.n = %s\n", c.n); ^ 6、小结:
二、总结: 好了,今天的分享就到这里,如果文章中有错误或者不理解的地方,可以交流互动,一起进步。 |