Forward declaration of nested types/classes in C++

前端 未结 7 1100
刺人心
刺人心 2020-11-22 10:45

I recently got stuck in a situation like this:

class A
{
public:
    typedef struct/class {...} B;
...
    C::D *someField;
}

class C
{
public:
    typedef          


        
7条回答
  •  渐次进展
    2020-11-22 11:01

    If you really want to avoid #including the nasty header file in your header file, you could do this:

    hpp file:

    class MyClass
    {
    public:
        template
        void doesStuff();
    };
    

    cpp file

    #include "MyClass.hpp"
    #include "Annoying-3rd-party.hpp"
    
    template<> void MyClass::doesStuff()
    {
        // ...
    }
    

    But then:

    1. you will have to specify the embedded type at call time (especially if your function does not take any parameters of the embedded type)
    2. your function can not be virtual (because it is a template)

    So, yeah, tradeoffs...

提交回复
热议问题