C朋友班和朋友会员功能

C朋友班和朋友会员功能,第1张

概述我正在学习C类中的朋友函数,朋友类和朋友成员函数; 现在,以下代码编译正常: #include <iostream>class A {public: friend class B; //friend void B::set(int i); //friend int B::get(); friend int function(A a); A(int i 我正在学习C类中的朋友函数,朋友类和朋友成员函数;
现在,以下代码编译正常:

@H_404_12@#include <iostream>class A {public: frIEnd class B; //frIEnd voID B::set(int i); //frIEnd int B::get(); frIEnd int function(A a); A(int i); voID set(int i); int get();private: int i;};A::A(int i) : i(i){}voID A::set(int i){ this->i = i;}int A::get(){ return i;}class B{public: B(A a); voID set(int i); int get();private: A a;};B::B(A a) : a(a){}voID B::set(int i){ a.i = i;}int B::get(){ return a.i;}int function(A a);int main(int argc,char *argv[]){ A a(0); std::cout << "in A i=" << a.get() << std::endl; a.set(10); std::cout << "in A i=" << a.get() << std::endl; B b(a); std::cout << "in B i=" << b.get() << std::endl; b.set(21); std::cout << "in B i=" << b.get() << std::endl; std::cout << "function returns " << function(a) << std::endl;}int function(A a){ return a.i;}

我能够给B级友谊并在A类中起作用“功能”,而无需前进声明B类或功能“功能”.
现在,如果我想给B类中的两个成员函数赋予友谊,那么如果我在定义类A之前没有定义类B,它就不起作用:

@H_404_12@#include <iostream>class B; // doesn't work,incomplete type (complete type needed)class A {public: //frIEnd class B; frIEnd voID B::set(int i); frIEnd int B::get(); frIEnd int function(A a); A(int i); voID set(int i); int get();private: int i;};A::A(int i) : i(i){}voID A::set(int i){ this->i = i;}int A::get(){ return i;}B::B(A a) : a(a){}voID B::set(int i){ a.i = i;}int B::get(){ return a.i;}int function(A a);int main(int argc,char *argv[]){ A a(0); std::cout << "in A i=" << a.get() << std::endl; a.set(10); std::cout << "in A i=" << a.get() << std::endl; B b(a); std::cout << "in B i=" << b.get() << std::endl; b.set(21); std::cout << "in B i=" << b.get() << std::endl; std::cout << "function returns " << function(a) << std::endl;}int function(A a){ return a.i;}

但是在定义A类之前我无法定义B类,所以我被卡住了.前向声明(未定义)B类也不起作用.

所以我的问题是:

1)为什么我不需要在友谊声明中转发声明函数或整个类,但是如果我需要指定该类的成员函数,我需要定义一个类?
我知道友情声明不是常识中的声明(它们只是授予访问权限,它们不会转发声明任何内容).

2)如何编译我的代码(除了将B中的A成员对象声明为A * a)?

解决方法 以下是朋友类的示例以及如何使用它.这是 cplusplus.com我发布这个的原因是因为你的例子并没有真正说明在c中正确使用友谊.我希望这能说明你应该如何/为什么要使用友谊,这可以导致你解决你的前瞻性声明问题.

@H_404_12@// frIEnd class#include <iostream>using namespace std;class Square;class Rectangle { int wIDth,height; public: int area () {return (wIDth * height);} voID convert (Square a);};class Square { frIEnd class Rectangle; private: int sIDe; public: Square (int a) : sIDe(a) {}};voID Rectangle::convert (Square a) { wIDth = a.sIDe; height = a.sIDe;}int main () { Rectangle rect; Square sqr (4); rect.convert(sqr); cout << rect.area(); return 0;}

In this example,class Rectangle is a frIEnd of class Square allowing
Rectangle’s member functions to access private and protected members
of Square. More concretely,Rectangle accesses the member variable
Square::sIDe,which describes the sIDe of the square.

There is something else new in this example: at the beginning of the
program,there is an empty declaration of class Square. This is
necessary because class Rectangle uses Square (as a parameter in
member convert),and Square uses Rectangle (declaring it a frIEnd).

FrIEndships are never corresponded unless specifIEd: In our example,
Rectangle is consIDered a frIEnd class by Square,but Square is not
consIDered a frIEnd by Rectangle. Therefore,the member functions of
Rectangle can access the protected and private members of Square but
not the other way around. Of course,Square Could also be declared
frIEnd of Rectangle,if needed,granting such an access.

Another property of frIEndships is that they are not transitive: The frIEnd of a frIEnd is not consIDered a frIEnd unless explicitly specifIEd.

总结

以上是内存溢出为你收集整理的C朋友班和朋友会员功能全部内容,希望文章能够帮你解决C朋友班和朋友会员功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1228869.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存