views:

93

answers:

2

Hi,

I know how to do an opaque object in C++ as following:

// my_class.hpp
class opaque_object;
class my_class {
    my_class();
    ~my_class();
    opaque_object *m_opaque_object;
};

// my_class.cpp
#include <my_class.hpp>
class opaque_object {
    // ...
};
my_class::my_class() { m_opaque_object = new opaque_object(); }
my_class::~my_class() {delete m_opaque_object; }

Now how to do it when the opaque object is an existing class template in a different namespace without including the header file of this one. The following code is not good, it is just here to illustrate my problem.

// my_class.hpp
class third::party::library::opaque_object<
    third::party::library::templated_class>;

class my_class {
    my_class();
    ~my_class();
    third::party::library::opaque_object<
        third::party::library::templated_class> *m_opaque_object;
};

// my_class.cpp
#include <my_class.hpp>
#include <third/party/library/opaque_object.hpp>
#include <third/party/library/template_class.hpp>

typedef third::party::library::opaque_object<
    third::party::library::templated_class> opaque_object;

my_class::my_class() { m_opaque_object = new opaque_object(); }
my_class::~my_class() {delete m_opaque_object; }

The actual class of my source code is even more templated than this example (4 arguments of template with some of them which are themselves templated class).

Since my_class.hpp is used quite everywhere in my project, the general compilation take a lot of time (5sec juste to include my_class.hpp for each cpp file) so I would like to avoid including opaque_object.hpp, template_class.hpp ... in my_class.hpp.

How can this be done ? Any comment, or idea to do it are welcome. Thanks in advance for your time.

A: 

Why not make it a template-template parameter (don't know if you need more parameters there :)

template <typename T, template <class> typename opaque>
class my_class
{
...
    opaque<T>* opaque_;
};
Nikolai N Fetissov
It's quite an elegant way to do it! but every time I will implement this object in my program, I will need to define the template argument if I am not wrong...
Phong
A: 

Need to declare each templated class as well as each class needed as for template (template argument) with the right namespace.

Phong