Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can I "forward declare" in C++?

I know I can do

class Foo;

and probably

struct Bar;

and global functions

bool IsValid(int iVal);

What about a typed enum? What about a typed enum within an undeclared class? What about a function with an undeclared class? What about a static member within an undeclared class? What about these within an unknown namespace? Am I missing anything else that can be forward declared?

like image 204
franji1 Avatar asked Sep 26 '10 21:09

franji1


People also ask

What can be forward declared?

In C++, Forward declarations are usually used for Classes. In this, the class is pre-defined before its use so that it can be called and used by other classes that are defined before this. Example: // Forward Declaration class A class A; // Definition of class A class A{ // Body };

Can you forward-declare a function?

To write a forward declaration for a function, we use a function declaration statement (also called a function prototype). The function declaration consists of the function header (the function's return type, name, and parameter types), terminated with a semicolon. The function body is not included in the declaration.

Why forward-declare instead of include?

A forward declaration is much faster to parse than a whole header file that itself may include even more header files. Also, if you change something in the header file for class B, everything including that header will have to be recompiled.

What is forward declaration in procedure?

This declaration makes that program available to be called by other programs even before the program definition. Remember that both procedures and functions have a header and a body. A forward declaration consists simply of the program header followed by a semicolon (;). This construction is called the module header.


1 Answers

You can forward declare

  • Templates, including partial specializations
  • Explicit specializations
  • Nested classes (this includes structs, "real" classes and unions)
  • Non-nested and local classes
  • Variables ("extern int a;")
  • Functions

If by "forward declaration" you strictly mean "declare but not define" you can also forward declare member functions. But you cannot redeclare them in their class definition once they are declared. You cannot forward-declare enumerations. I'm not sure whether I missed something.

Please note that all forward declarations listed above, except partial and explicit specializations, need to be declared using an unqualified name and that member functions and nested classes can only be declared-but-not-defined in their class definition.

class A { };
class A::B; // not legal

namespace A { }
void A::f(); // not legal

namespace A { void f(); } // legal

class B { class C; }; // legal
class B::C; // declaration-only not legal

class D { template<typename T> class E; };
template<typename T> class D::E<T*>; // legal (c.f. 14.5.4/6)
like image 172
Johannes Schaub - litb Avatar answered Oct 21 '22 12:10

Johannes Schaub - litb