class foo; in header file

后端 未结 9 2104
执笔经年
执笔经年 2021-01-20 08:36

Is some one able to explain why header files have something like this?

class foo; // This here?
class bar
{
   bar();

};

Do you need an in

9条回答
  •  没有蜡笔的小新
    2021-01-20 09:02

    this is a forward declaration of the class. In my experience, this is typically done when you have a circular dependency.. for example

    in foo.h
    --------
    #include "bar.h"
    
    class foo
    {
    public:
        foo(bar *bar);
    private:
        foo *m_foo;
    };
    
    and in bar.h
    ------------
    class foo;
    class bar
    {
    public:
        void methodFooWillCall();
    protected:
        std::list myFoos;
    }
    

提交回复
热议问题