1

首先我想展示工作代码,然后解释我想如何改变事情。这是简单的 boost multi_index 示例:

//main.cpp    
    #include <boost/multi_index_container.hpp>
    #include <boost/multi_index/ordered_index.hpp>
    #include <boost/multi_index/identity.hpp>
    #include <boost/multi_index/member.hpp>
    #include <string>

    struct employee
    {
        int         id;
        std::string name;

        employee(int id, const std::string& name) :id(id), name(name){}

        bool operator<(const employee& e)const{ return id<e.id; }
    };

    typedef boost::multi_index::multi_index_container<
        employee,
        boost::multi_index:: indexed_by<
        // sort by employee::operator<
        boost::multi_index:: ordered_unique< boost::multi_index:: identity<employee> >,

        // sort by less<string> on name
        boost::multi_index::ordered_non_unique<boost::multi_index::member<employee, std::string, &employee::name> >
        >
    > employee_set;

    int main()
    {
        employee_set es;
        es.insert(employee(0, "Bob"));
    }

想象一下如果 main.cpp 是另一个没有 boost 依赖的模块。我想了解如何:

包括一些头文件,其中 boost multiindex 容器类被前向声明到 main.cpp 在附加的 .cpp 文件中定义员工的多索引容器我尝试了很多变体,但如果这可行,则没有。有可能创造出这样的东西吗?

//notmain.cpp
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include "notmain.h"

typedef boost::multi_index::multi_index_container<
    employee,
    boost::multi_index::indexed_by<
    // sort by employee::operator<
    boost::multi_index::ordered_unique< boost::multi_index::identity<employee> >,

    // sort by less<string> on name
    boost::multi_index::ordered_non_unique<boost::multi_index::member<employee, std::string, &employee::name> >
    >
> employee_set;

现在是 h.file 我需要填写容器的前向声明(或显式启动)。我可能会误解这些术语,但我是 C++ 和 boost 的新手。

//notmain.h

#include <string>

/*
    Some how here I need forward declaration or explicit initiation of boost container 
    class employee_set ???

*/

struct employee
{
    int         id;
    std::string name;

    employee(int id, const std::string& name) :id(id), name(name){}

    bool operator<(const employee& e)const{ return id<e.id; }
};

这是最终目标。我想提醒一下 main.cpp 被想象为另一个模块的 .cpp ,没有提升依赖。

//main.cpp
#include "notmain.h"

int main()
{
    employee_set es;
    es.insert(employee(0, "Bob"));
}
4

1 回答 1

3

如果该类型是类的可见接口的一部分,则必须包含该类所依赖的任何标头,这是没有办法的。如果您真的不希望它成为可见界面的一部分,请考虑使用 pImpl 成语:

公共标头

#if !defined(MYCLASS_PUBLIC_H_)
#define MYCLASS_PUBLIC_H_

struct MyClassImpl;
class MyClass {
  MyClassImpl * pImpl;
public:
  void SomeOperation();
};
#endif

实现标头:

#if !defined(MYCLASS_IMPL_H_)
#define MYCLASS_IMPL_H_
#include <private_type.h>
#include "MyClass.h"
struct MyClassImpl
{
  void Operation();

private:
  SomePrivateType member;
};
#endif

实现文件:

#include "MyClassImpl.h"
void MyClass::SomeOperation()
{
  pImpl->Operation();
}

void MyClassImpl::Operation()
{
  // do something with 'member'
}

只看到公共接口的代码:

#include "MyClass.h"
void foo()
{
  MyClass inst;
  inst.SomeOperation();
}
于 2017-12-27T19:08:32.370 回答