nionios
nionios

Reputation: 189

Forward declaration in header only development

I would like to understand the pros/cons of forward declaration for classes/structs that are in different files in header only development. I understand that using forward declaration in normal code-development (implementation in .cpp files), but what does it actually bring in header only development?

Upvotes: 0

Views: 338

Answers (1)

eerorika
eerorika

Reputation: 238461

The pros and cons are same as with non header only programming. If class b depends on definition of class a, but class a depends on the declaration of class b, then it is a good practice to forward declare class b before definition of class a because otherwise the program would be ill-formed. (Technically you could forward declare within definition of a by using an elaborated type specifier, but some people don't like that for stylistic reasons).

Upvotes: 2

Related Questions