Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a forward declaration in a function declaration allowed?

Tags:

People also ask

When can you use forward declaration?

You will usually want to use forward declaration in a classes header file when you want to use the other type (class) as a member of the class. You can not use the forward-declared classes methods in the header file because C++ does not know the definition of that class at that point yet.

What is forward declaration of a class and why is it often essential when using friend functions?

So a 'Forward Declaration' is just what it says on the tin. It's declaring something in advance of its use. Generally you would include forward declarations in a header file and then include that header file in the same way that iostream is included.

Are forward declarations good?

The Google style guide recommends against using forward declarations, and for good reasons: If someone forward declares something from namespace std, then your code exhibits undefined behavior (but will likely work).

Why do you use forward declaration in PL SQL?

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.


While reading about the visitor pattern I ran into this snippet of code:

virtual void visit(class Composite *, Component*) = 0;

This is a member function, and it seems to be forward declaring the class Composite inside its parameters. I tried this with just a normal function, like so:

void accept(class A a);

for some class A that I haven't declared or defined yet and the code worked fine. Why is this allowed? How, if at all, is it different from forward declaring before the line? Has anything changed recently in the standard in regards to this?


Many people are claiming this is a leftover of C, but then why does this code compile fine in C++, but not C?

#include <stdio.h>
int process(struct A a);

struct A{
    int x;
};

int process(struct A a){
    return a.x;
}

int main(void)
{
    struct A a = {2};
    printf("%d", process(a));
    return 0;
}