Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;访问好友类中的私有成员_C++_Private Members_Friend Class - Fatal编程技术网

C++ C++;访问好友类中的私有成员

C++ C++;访问好友类中的私有成员,c++,private-members,friend-class,C++,Private Members,Friend Class,我有两个类(firstClass和secondClass),firstClass是secondClass的朋友,并且有一个私有嵌套无序映射,我想在secondClass的函数中访问它。 所以基本上代码是这样的: class secondClass; typedef unordered_map STable<unsigned, unordered_map<unsigned, double> > NESTED_MAP; class firs

我有两个类(firstClass和secondClass),firstClass是secondClass的朋友,并且有一个私有嵌套无序映射,我想在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;

        }
第二类;
typedef无序_图稳定嵌套_图;
头等舱{
朋友班二等;
无效myfunc1(第二类*sc){
sc->myfunc2(&STable);
}
私人:
嵌套映射稳定;
};
二等舱{
公众:
void myfunc2(嵌套映射*st){
//这里我想在STable中插入一些元素。
//比如:
st[1][2]=0.5;
}
};
int main(){
一级fco;
二级上海合作组织;
fco.myfunc1(和sco);
返回0;
}
我知道这应该是微不足道的,但我不知道如何解决它。
有什么想法吗?(我更改了代码和问题以使其更清楚)

允许friend类访问任何私有成员,因此您可以简单地调用方法并修改属性,就像它们是公共的一样

文件上说:

友元声明出现在类主体中,并向出现友元声明的类的私有和受保护成员授予函数或其他类访问权限

也就是说,通过查看您的示例,我宁愿更改放置friend关键字的位置,因为在我看来myfunc2不应该是公共的

下面是一个例子,我应用了上述建议,展示了如何处理来自朋友类的私人成员:

#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;
}
#包括
使用名称空间std;
头等舱;
二等舱{
朋友一流;
私人:
无效myfunc2(无序映射和映射){
map[1]=0.5;
}
};
头等舱{
公众:
无效myfunc1(第二类*sc){
//这里firstClass正在访问一个私有成员
//二等舱,因为它可以这样做
//做朋友
sc->myfunc2(稳定);
}
私人:
无序地图稳定;
};
int main(){
一级fco;
二级上海合作组织;
fco.myfunc1(和sco);
返回0;
}

如果一个类在另一个类中,你将如何访问公共数据成员?我不明白你的意思。@哈基姆让第二个类成为朋友意味着你可以像访问
公共
成员一样访问其
私有
成员。非常感谢skypjack。因为我太累了,我完全忽略了这一点。它起作用了。