I know it's possible to create a friend function in C++:
我知道可以在C ++中创建一个友元函数:
class box
{
friend void add(int num);
private:
int contents;
};
void add(int num)
{
box::contents = num;
return;
}
But is there a way to create friend classes?
但有没有办法创建朋友类?
NB: I know there are probably a lot of errors in this code, I don't use friend functions and am still pretty new to the language; if there are any, please tell me.
注意:我知道这段代码中可能存在很多错误,我不使用友元函数,而且对语言来说还是新手;如果有,请告诉我。
3 个解决方案
#1
Yup - inside the declaration of class Box
, do
是的 - 在类Box的声明中,做
friend class SomeOtherClass;
All member functions of SomeOtherClass
will be able to access the contents
member (and any other private members) of any Box
.
SomeOtherClass的所有成员函数都可以访问任何Box的内容成员(以及任何其他私有成员)。
#2
By the way, a design guideline is that, if a class is close enough to be declared a friend, then it's close enough to be declared as a nested class in the same header file, for example:
顺便提一下,设计指南是,如果一个类足够接近被宣布为朋友,那么它足够接近于在同一个头文件中被声明为嵌套类,例如:
class Box
{
class SomeOtherClass
{
//some implementation that might want to access private members of box
};
friend class SomeOtherClass;
private:
int contents;
};
If you don't want to declare the other class as a nested class in the same header file, then perhaps you shouldn't (although you are able to) declare it a friend.
如果你不想在同一个头文件中将另一个类声明为嵌套类,那么也许你不应该(虽然你能够)将它声明为朋友。
#3
In your code, you are currently using the box member 'contents' as a static member in the add function ( box::contents = num; )
在您的代码中,您当前正在使用box成员'contents'作为add函数中的静态成员(box :: contents = num;)
You should either declare contents as being static, like so: (you also have to initialize it then..)
您应该将内容声明为静态,如下所示:(您还必须初始化它然后..)
class box
{
friend void add(int num);
private:
static int contents;
};
int box::contents;
void add(int num)
{
box::contents = num;
return;
}
or, change your add function to accept a box object and an int:
或者,更改您的add函数以接受box对象和int:
class box
{
friend void add(box *b, int num);
private:
int contents;
};
void add(box *b, int num)
{
b->contents = num;
return;
}
#1
Yup - inside the declaration of class Box
, do
是的 - 在类Box的声明中,做
friend class SomeOtherClass;
All member functions of SomeOtherClass
will be able to access the contents
member (and any other private members) of any Box
.
SomeOtherClass的所有成员函数都可以访问任何Box的内容成员(以及任何其他私有成员)。
#2
By the way, a design guideline is that, if a class is close enough to be declared a friend, then it's close enough to be declared as a nested class in the same header file, for example:
顺便提一下,设计指南是,如果一个类足够接近被宣布为朋友,那么它足够接近于在同一个头文件中被声明为嵌套类,例如:
class Box
{
class SomeOtherClass
{
//some implementation that might want to access private members of box
};
friend class SomeOtherClass;
private:
int contents;
};
If you don't want to declare the other class as a nested class in the same header file, then perhaps you shouldn't (although you are able to) declare it a friend.
如果你不想在同一个头文件中将另一个类声明为嵌套类,那么也许你不应该(虽然你能够)将它声明为朋友。
#3
In your code, you are currently using the box member 'contents' as a static member in the add function ( box::contents = num; )
在您的代码中,您当前正在使用box成员'contents'作为add函数中的静态成员(box :: contents = num;)
You should either declare contents as being static, like so: (you also have to initialize it then..)
您应该将内容声明为静态,如下所示:(您还必须初始化它然后..)
class box
{
friend void add(int num);
private:
static int contents;
};
int box::contents;
void add(int num)
{
box::contents = num;
return;
}
or, change your add function to accept a box object and an int:
或者,更改您的add函数以接受box对象和int:
class box
{
friend void add(box *b, int num);
private:
int contents;
};
void add(box *b, int num)
{
b->contents = num;
return;
}