如何正确访问好友功能?(How to access friend functions correctly?)
我正在尝试访问朋友的方法,如下所示。 我有4个文件。 两个标题:
标题
ah
:#include "b.h" class A { public: A(); ~A(); void testStuff(int i); };
标题
bh
:#include "a.h" class A; class B { friend class A; public: B(); ~B(); friend void doStuff(int i); };
两个包括一些代码,
a.cpp
:#include "a.h" void A::testStuff() { B b {B()}; b->doStuff(1); }
b.cpp
:#include "b.h" void B::doStuff(int i) { m_stuff = i; }
如果我删除关键字
friend
一切正常。 但是如果我将doStuff()
声明为朋友,我会收到以下错误:error: ‘class B’ has no member named ‘doStuff’
我尝试过这个教程。 我错过了什么?
I'm trying access a friend's method as defined the following. I have 4 files. Two header:
Header
a.h
:#include "b.h" class A { public: A(); ~A(); void testStuff(int i); };
Header
b.h
:#include "a.h" class A; class B { friend class A; public: B(); ~B(); friend void doStuff(int i); };
And two including some code,
a.cpp
:#include "a.h" void A::testStuff() { B b {B()}; b->doStuff(1); }
b.cpp
:#include "b.h" void B::doStuff(int i) { m_stuff = i; }
If I remove the keyword
friend
everything works fine. But if I declaredoStuff()
as a friend, I get the following error:error: ‘class B’ has no member named ‘doStuff’
I tried following this tutorial. What do I miss?
原文:https://stackoverflow.com/questions/24087193
最满意答案
您对
friend
功能的理解是错误的 。 如果将函数friend
声明为另一个类,则表示friend
函数可以访问该类的私有成员,这并不意味着friend
函数成为类成员 。这里
doStaff()
是B
friend
,而不是B
的成员。 但是你在B
的对象上使用它,因此编译器说它不是该类的成员。 使用friend
给doStaff
权限访问B
的private
成员。您所关注的教程清楚地说明了这一点。
duplicate()
方法是Rectangle
的朋友,因此它可以访问private
成员的width
和height
,但duplicate()
不用作Rectangle
的类方法。总之,你的问题是错误的。 您可以像任何免费 (非类方法)功能一样访问
friend
功能。 问题是friend
函数如何访问它所属的类的成员,并且如上所述,它可以访问该类中的所有内容,这是拥有朋友函数的动机,即允许访问类的私有成员一个局外人。类似地,
friend
类可以访问它所熟悉的类的所有内容。因此,对于您的问题,要么从
doStaff
移除friend
以使其成为B
的成员。 但我想你的意图是使用friend
方法,在这种情况下你可以做到以下几点:void doStuff(B b, int i) { b.m_stuff = i; // it is friend function of B, so can access m_staff }
或者你可以成为
A
doStaff()
成员然后你可以写class A{ //other members void doStaff(B& b, int i){ b.m_staff=i;} void A::testStuff() { B b {B()}; doStuff(b,1); //although u can just write "b.m_staff = 1;" right here }
Your understanding regarding
friend
function is wrong. If you declare a functionfriend
to another class, it means thefriend
function can access private members of that class, it does not mean thefriend
function becomes a class member.Here
doStaff()
isfriend
toB
, not a member ofB
. But you are using it on an object ofB
, hence compiler saying that it is not member of that class. usingfriend
givesdoStaff
permission to accessB
'sprivate
members in it.The tutorial you are following says it clearly.
duplicate()
method is a friend toRectangle
, hence it can access theprivate
memberswidth
andheight
, butduplicate()
is not used as a class method ofRectangle
.In summary, your question is wrong. You can access a
friend
function just like any free (non-class method) function. The question is howfriend
functions accesses the member of a class to which it is friend, and as said, it can access everything in that class, which is the motivation of having friend functions, namely to give access of private members of a class to a outsider.Similarly a
friend
class can access everything of the class to which it is friend.So for your problem, either you remove
friend
fromdoStaff
to make it a member ofB
. But I guess your intention was to usefriend
method, in that case you can do the following:void doStuff(B b, int i) { b.m_stuff = i; // it is friend function of B, so can access m_staff }
or you can make
doStaff()
member ofA
and then you can writeclass A{ //other members void doStaff(B& b, int i){ b.m_staff=i;} void A::testStuff() { B b {B()}; doStuff(b,1); //although u can just write "b.m_staff = 1;" right here }