Forward declaration as struct vs class

后端 未结 1 828
花落未央
花落未央 2020-12-15 05:01

I found a mistake in a C++ forward declaration of a class, which was wrongly declared as struct Book instead of class Book. I think Book used to be

相关标签:
1条回答
  • 2020-12-15 05:37

    struct and class are completely interchangeable as far as forward declarations are concerned. Even for definitions, they only affect the default access specifier of the objects members, everything else is equivalent. You always define "classes" of objects.

    The only place where struct must be used over class, is when forward declaring opaque data for c bindings.


    Regarding your edit:

    I know the differences between class/struct regarding default public/private but I'm asking specifically about the forward declarations and possible consequences of swapping them.

    Visual C++ produces warning C4099. It does it, because the name decoration for its functions incorporates the keyword you used. Therefore, programs may fail to link properly. So perfectly standard compliant code may not link when using VC++ (A bonehead move on Microsoft's part, AFAIC).

    A discussion of this warning, as well as why it can be ignored if you are disciplined, can be found here

    0 讨论(0)
提交回复
热议问题