Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward declarations for variables?

I have some C code that I have to port to C++. The code has a structure

struct A { 
    ...
    struct A * myPtr;
}

And now two global arrays are declared and initialized like this:

//Forward declaration of Unit
struct A Unit[10];

struct A* ptrUnit[2] = { Unit, Unit+7 };
struct A Unit[10] = { { .., &ptrUnit[0] }, 
                      ... };

Now while this works fine in C, it gives an error in C++ (variable redeclared). Aren't variables allowed to be forward-declared in C++?

like image 697
Omair Avatar asked Sep 15 '10 05:09

Omair


People also ask

What is the purpose of forward declarations?

A forward declaration allows us to tell the compiler about the existence of an identifier before actually defining the identifier. In the case of functions, this allows us to tell the compiler about the existence of a function before we define the function's body.

What is meant by forward declaration?

In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition.

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 in Objective C?

Forward declarations are mainly to avoid circular imports, where one file imports another file which imports the first file etc. Basically when you import a file, contents of the file are substituted at the point of import when you build your project, which is then fed to the compiler.


2 Answers

In C++, a variable declaration must be prefixed with extern:

extern A Unit[10];

// ...

A Unit[10] = { ... };

(Note that in C++ you can omit the leading struct.)

like image 66
sbi Avatar answered Oct 07 '22 01:10

sbi


struct A Unit[10] is not a forward declaration of a variable. The term "forward declaration" normally refers to non-defining declarations, while struct A Unit[10] is a definition. So in your code you are defining Unit multiple times in the same source file. In C language it is allowed, since in C definitions without an initializer are tentative definitions. They may occur many times in the same translation unit. In C++ there's no such thing as tentative definition. In C++ multiple definitions are always illegal.

If you want a genuine forward declaration for a variable, you have to use the keyword extern

extern struct A Unit[10];

This will work in both C and C++. However, as a side effect, this will give Unit external linkage. If you need a variable with internal linkage, then you are out of luck in C++, since in C++ it is not possible to forward-declare a variable with internal linkage. Meanwhile, in C tentative definitions will still help you to achieve that.

like image 23
AnT Avatar answered Oct 07 '22 01:10

AnT