Forward.cpp
TCP1, p. 163-164
download
// Forward function & data declarations
#include <iostream>
using std::cout;
using std::endl;
// This is not actually external, but the
// compiler must be told it exists somewhere:
extern int i; // data declaration, not definition
extern int i; // can be declared multiple times
// extern static int i; // compile error: conflicting specifiers
extern void func(); // function declaration
extern void func(); // can be declared multiple times
// extern static void func(); // compile error: conflicting specifiers
int main()
{
i = 0;
func(); // 1
return 0;
}
int i; // The data definition
// int i; // compile error: redefinition
void func() // function definition
{
i++;
cout << i << endl;
}
// void func() {} // compile error: redefinition
/*
g++ Forward.cpp -o Forward
./Forward
1
*/
Comments
Post a Comment