class foo; in header file

后端 未结 9 2003
执笔经年
执笔经年 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 08:45

    It's a forwards declaration of the class 'foo'. It allows you to declare pointers and references to the class, but not use it (eg. call members or determine its size), because it's not yet defined! It must later be followed up with a full, normal declaration (class foo { ... };).

    It's useful for things like declaring two classes which hold pointers to each other, which otherwise would be impossible to set up.

提交回复
热议问题