How to call a private function via friend function

2019-02-27 05:13发布

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条回答
Lonely孤独者°
2楼-- · 2019-02-27 05:17

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();
    }
};
查看更多
登录 后发表回答