'Define a friend function for a static class method?

Does someone know how to let outside world access private static methods? The only way I could think of is via friend function, but that doesn't seem to work.

Header file here:

#pragma once

#include <iostream>

class MyClass {
 public:
    friend void AtFork();

 private:
    static void Disp() { std::cout << "hello world" << std::endl; }
};

impl file here:

#include "test.h"

namespace {

void AtFork() {
    MyClass::Disp();
}

}

int main() {
    AtFork();
}

The error message is:

test.cc:6:11: error: 'Disp' is a private member of 'MyClass'
        MyClass::Disp();
                 ^
./test.h:10:15: note: declared private here
        static void Disp() { std::cout << "hello world" << std::endl; }

context: to get fork-safe (due to a bunch of legacy code), I have a reinitialize a 3rd library by binding a fork hook, and I don't think it should be exposed to public.

For the above example, it's let AtFork being able to call MyClass::Disp.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source