Udostępnij za pośrednictwem


Redeclaration of typedef Names

The typedef declaration can be used to redeclare the same name to refer to the same type. For example:

// FILE1.H
typedef char CHAR;

// FILE2.H
typedef char CHAR;

// PROG.CPP
#include "file1.h"
#include "file2.h"   // OK

The program PROG.CPP includes two header files, both of which contain typedef declarations for the name CHAR. As long as both declarations refer to the same type, such redeclaration is acceptable.

A typedef cannot redefine a name that was previously declared as a different type. Therefore, if FILE2.H contains

// FILE2.H
typedef int CHAR;     // Error

the compiler issues an error because of the attempt to redeclare the name CHAR to refer to a different type. This extends to constructs such as:

typedef char CHAR;
typedef CHAR CHAR;      // OK: redeclared as same type

typedef union REGS      // OK: name REGS redeclared
{                       //  by typedef name with the
    struct wordregs x;  //  same meaning.
    struct byteregs h;
} REGS;

See Also

Reference

typedef Specifier