开发者

how to not include boost header file in my own header file

开发者 https://www.devze.com 2023-04-13 05:59 出处:网络
I am writing a class In the class, I use boost::graph::adjacent_list as a private member. but I don\'t want my header file inc开发者_StackOverflow社区lude boost header file, because any other file i

I am writing a class

In the class, I use boost::graph::adjacent_list as a private member.

but I don't want my header file inc开发者_StackOverflow社区lude boost header file, because any other file include my header file will need to include boost header file.

Is there a way can avoid include boost header file in my own header file.

I have tried forward declaration, but fails.


You can't do this directly, but you can probably solve the underlying problem of not propagating an implementation detail (boost) by pimpling your class. This essentially means that you forward declare a pointer to your implementation details, which are then fully implemented just in a source file.


If your header file is definition only, and is fully implemented in a cpp file (i.e. the header file doesn't do anything with boost::graph::adjacent_list then in your header, you can

  • Forward declare a bgalWrapper struct.
  • Have a pointer to that struct instead of boost::graph::adjacent_list
  • In the CPP file that implements the header file, declare the new bgalWrapper struct (which only has a boost::graph::adjacent_list member)
  • Create an instance of the struct in the classes ctor (and - obviously
    • clean up in the dtor).

Now each class can have a reference to the boost item without the header needing to know about boost.

There's probably a name (and wikipage) for this pattern, but it's years since I programmed C++ seriously.

Hope this helps.

Update What I've described is a partial implementation of the PIMPL Idiom
Thanks Mark B


I didn't test it, but can't you define the list as a pointer variable? Then the forward declaration should succeed. You'll have to nest the namespaces though:

using namespace boost::graph;
class adjacent_list;
class MyClass {
    private adjacent_list<foo, bar> m_list;
}

Or perhaps:

namespace boost {
    namespace graph {
        class adjacent_list;
    }
}
class MyClass {
    private adjacent_list<foo, bar> m_list;
}

Then in your implementation file, just use it as a pointer:

#include <boost/graph/adj_list_serialize.hpp>
MyClass::MyClass() {
    m_list = new adjacency_list<foo, bar>();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号