In a .h if I have:
#pragma once
#include <xxxx.h>
#include "yyyy.h"
class AAAAAA;
class BBBBBB;
class ZZZZZZ
{
public:
// etc
};
using class AAAAAA; is forward declaring a class right?
Why would one do this?
Is it needed?
What are the benefits? Drawbacks?
Because the compiler only knows names that have been declared. So if you want to use a class, you have to declare it. But if its definition depends on its user, a forward declaration can suffice if the user doesn’t depend on the definitin of the class in turn, but just on its declaration (= name).
In particular, if the user just needs a pointer or referece, it doesn’t depend on the definition. But in the cases listed (which do not claim to be exclusive, since it’s a standard non-normative excerpt), the user depends on the definition of class T if
or
type pointer to T or reference to T using an implicit conversion (Clause 4), a dynamic_cast (5.2.7) or
a static_cast (5.2.9), or
In these cases, a forward declaration will not suffice and you need to fully define it.
Yes, in the cases where it is needed it is needed. In the following case it is, because both classes refer to each other.
The member function definition required not only a declaration but also the definition of class A.
The benefits are that your program compiles. The drawback is that you have polluted the scope with just another name. But that’s the necessary evil of it.