请指教以下C++语法及其意义!(可能是超载的情况)

lin*_*asy 2 c++ syntax pointers overloading function

我正在阅读遗产,并发现C++的以下语法很奇怪:

类定义如下:

class thread_shared : public master
{
public:
    thread_shared() 
    {
        thread_id = 0;
    }
    int thread_id;
    std::string title;
    std::string (*text2html)(std::string const &);  // What's this declaration?

};
Run Code Online (Sandbox Code Playgroud)

和text2html的定义为:

namespace {
    std::string text2html(std::string const &s)
    {
        std::string tmp=cppcms::util::escape(s);
        std::string res;
        res.reserve(tmp.size());
        for(unsigned i=0;i<tmp.size();i++) {
            if(tmp[i]=='\n') {
            res+="<br />";
            }
            res+=tmp[i];
        }
        return res;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后使用如下:

c.id = 1; // any integer
c.title = "some string";        
c.text2html = text2html;   // whats this??
Run Code Online (Sandbox Code Playgroud)

where cthread_shared上面声明的类的实例.

如上所述,有人可以解释我的声明如下:

std::string (*text2html)(std::string const &); 然后 c.text2html = text2html;

上面的代码到底是做什么的?

Dav*_*eas 11

std::string (*text2html)(std::string const &);  // What's this declaration?
Run Code Online (Sandbox Code Playgroud)

这是一个函数指针定义.它声明一个变量text2html是指向具有签名的函数的指针std::string (std::string const &).

c.text2html = text2html;   // whats this??
Run Code Online (Sandbox Code Playgroud)

即设置指针以引用text2html具有适当签名的函数.右侧是函数标识符并衰减到&text2html,在这种情况下,它解析为未命名的命名空间中的地址.

请注意,您可以将其设置为具有相同签名的任何函数,名称的巧合只是巧合:

std::string foo( std::string const & ) {}
c.text2html = &foo;  // & optional
Run Code Online (Sandbox Code Playgroud)