Search code examples
c++fork

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.


Solution

  • The issue in your code is the anonymous namespace. The AtFork in main.cpp can only be accessed within main.cpp. The AtFork you declared as friend is a different one. Do not use an anonymous namespace and the code compiles fine:

    #include <iostream>
    
    class MyClass {
     public:
        friend void AtFork();
    
     private:
        static void Disp() { std::cout << "hello world" << std::endl; }
    };
    
    
    //namespace {
    
    void AtFork() {
        MyClass::Disp();
    }
    
    //}
    
    int main() {
        AtFork();
    }
    

    Though you can place AtFork in a (named) namespace if you like:

    #include <iostream>
    
    
    namespace foo { void AtFork();}
    
    class MyClass {
     public:
        friend void foo::AtFork();
    
     private:
        static void Disp() { std::cout << "hello world" << std::endl; }
    };
    
    
    namespace foo{
    
    void AtFork() {
        MyClass::Disp();
    }
    
    }
    
    int main() {
        foo::AtFork();
    }