How to call a private function via friend function?

℡╲_俬逩灬. 提交于 2020-01-11 09:56:06

问题


Hello I am trying to access a private member function is Gtest. The code looks somewhat similar to this. So, how can I access static void Pri_fun?

using namespace std;
class test{

};
class abc{
public:
    friend class test;
private:
    static void Pri_fun()
        {
        cout << "private fun called \n";
        }
};
int main()
{
    abc ab;
    test *abd;
    abd->Pri_fun();
}

回答1:


Since it's a static function, you should access it via the class name:

abc::Pri_fun();

You should make a caller function though, or call it from the friend class' constructor:

class test{
public:
    void foo() 
    {
        abc::Pri_fun();
    }
};

or

class test{
public:
    test() 
    {
        abc::Pri_fun();
    }
};


来源:https://stackoverflow.com/questions/12953107/how-to-call-a-private-function-via-friend-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!