Why include a header and forward declare the class contained in the same cpp file?

前端 未结 4 551
难免孤独
难免孤独 2021-01-13 08:14

I\'ve been looking at the Fear SDK for my university project, but have noticed some code like so:

Foo.h

class Foo
{
    public:
        int iSomethin         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-13 08:56

    The original class Foo; may have been vestigial.

    Remember that, if the source only uses pointers to Foo class [and does not actually try to create Foo objects or dereference Foo pointers], you dont need to define the class before using it.

    Without seeing the code, I'd hazard the guess that the original version of bar.cpp had code that did not require the definition of foo

    I use forward declarations in large projects to reduce compile time. compile time is not a problem when it takes a second, but when projects take an hour to build every second helps :)

提交回复
热议问题