4

下面是头文件中的一些简化代码,其中声明了自由函数但未定义,向量既声明又定义。

cpp 文件包含自由函数的实现。

我想知道是否有办法在头文件中声明向量并将定义放在 cpp 文件中。

// my-file.h

namespace MyNamespace
{
    bool foo(const std::string& name, const std::string& value);
    void bar(const std::string& name, const std::string& value);

    const std::vector<std::function<void(const std::string&, const std::string&)>> m_myVector
    {
        foo,
        bar,
        [](const std::string& name, const std::string& value)
        {
            // do some stuff
        }
    };

} // MyNamespace
4

1 回答 1

5

您可以在标头中将 const 变量声明为:

extern
const std::vector<std::function<void(const std::string&, const std::string&)>> m_myVector;
于 2015-07-16T11:15:20.347 回答