Cross dependencies without forward declaring all used functions?

后端 未结 2 367
不思量自难忘°
不思量自难忘° 2021-01-16 02:27

I have class A (in A.h) which depends on class B in (B.h) and vice versa. Forward declaring the used functions works, but this means I have to update everywhere where I forw

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 02:41

    The best way is to have a forward declaration header:

    a.fwd.h

    #pragma once
    class A;
    

    a.h

    #pragma once
    #include "a.fwd.h"
    #include "b.fwd.h"
    class A
    {
        A(B*);
    };
    

    etc.

    This way, each class provides its own forward declarations - localised alongside the header where it belongs - checked for consistency with the real declarations and definitions by including the forward declaration header in the header, and the header in the implementation.

提交回复
热议问题