简体   繁体   中英

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 .

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM