C++ 访问好友中的私有成员 class
C++ Accessing a private member in a friend class
我有 2 个 类(firstClass 和 secondClass),其中 firstClass 是 secondClass 的友元,并且有一个私有嵌套 unordered_map,我想在 secondClass 的函数中访问它。
所以基本上代码是这样的:
class secondClass;
typedef unordered_map STable<unsigned, unordered_map<unsigned, double> > NESTED_MAP;
class firstClass{
friend class secondClass;
void myfunc1(secondClass* sc){
sc->myfunc2(&STable);
}
private:
NESTED_MAP STable;
};
class secondClass{
public:
void myfunc2(NESTED_MAP* st){
//Here I want to insert some elements in STable.
//Something like:
st[1][2]=0.5;
}
};
int main(){
firstClass fco;
secondClass sco;
fco.myfunc1(&sco);
return 0;
}
我知道这应该是微不足道的,但我不知道如何解决。
任何的想法? (我更改了代码和问题以使其更清楚)
朋友 class 可以访问任何私有成员,因此您可以简单地调用方法和修改属性,就像它们是 public 时所做的那样。
Here 文档,它说:
The friend declaration appears in a class body and grants a function or another class access to private and protected members of the class where the friend declaration appears.
也就是说,通过查看您的示例,我宁愿更改放置 friend 关键字的位置,因为在我看来 myfunc2 不应该是 public.
它遵循了一个我应用上述建议的例子,它展示了如何处理来自朋友 class:
的 private 成员
#include<unordered_map>
using namespace std;
class firstClass;
class secondClass{
friend class firstClass;
private:
void myfunc2(unordered_map<unsigned,double>& map){
map[1]=0.5;
}
};
class firstClass{
public:
void myfunc1(secondClass* sc){
// here firstClass is accessing a private member
// of secondClass, for it's allowed to do that
// being a friend
sc->myfunc2(STable);
}
private:
unordered_map<unsigned,double> STable;
};
int main(){
firstClass fco;
secondClass sco;
fco.myfunc1(&sco);
return 0;
}
我有 2 个 类(firstClass 和 secondClass),其中 firstClass 是 secondClass 的友元,并且有一个私有嵌套 unordered_map,我想在 secondClass 的函数中访问它。 所以基本上代码是这样的:
class secondClass;
typedef unordered_map STable<unsigned, unordered_map<unsigned, double> > NESTED_MAP;
class firstClass{
friend class secondClass;
void myfunc1(secondClass* sc){
sc->myfunc2(&STable);
}
private:
NESTED_MAP STable;
};
class secondClass{
public:
void myfunc2(NESTED_MAP* st){
//Here I want to insert some elements in STable.
//Something like:
st[1][2]=0.5;
}
};
int main(){
firstClass fco;
secondClass sco;
fco.myfunc1(&sco);
return 0;
}
我知道这应该是微不足道的,但我不知道如何解决。 任何的想法? (我更改了代码和问题以使其更清楚)
朋友 class 可以访问任何私有成员,因此您可以简单地调用方法和修改属性,就像它们是 public 时所做的那样。
Here 文档,它说:
The friend declaration appears in a class body and grants a function or another class access to private and protected members of the class where the friend declaration appears.
也就是说,通过查看您的示例,我宁愿更改放置 friend 关键字的位置,因为在我看来 myfunc2 不应该是 public.
它遵循了一个我应用上述建议的例子,它展示了如何处理来自朋友 class:
的 private 成员#include<unordered_map>
using namespace std;
class firstClass;
class secondClass{
friend class firstClass;
private:
void myfunc2(unordered_map<unsigned,double>& map){
map[1]=0.5;
}
};
class firstClass{
public:
void myfunc1(secondClass* sc){
// here firstClass is accessing a private member
// of secondClass, for it's allowed to do that
// being a friend
sc->myfunc2(STable);
}
private:
unordered_map<unsigned,double> STable;
};
int main(){
firstClass fco;
secondClass sco;
fco.myfunc1(&sco);
return 0;
}