c – typedef是一个定义吗?

前端之家收集整理的这篇文章主要介绍了c – typedef是一个定义吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我真的很困惑.我正在阅读Bjarne Stroustrup的TC PL(特别版,第19版 – 2010年9月).让我引用这本书的一部分,突出我的困惑:
char ch;
string s;
int count = 1;
const double pi = 3.1415926535897932385;
extern int error_number;

const char* name = "Njal";
const char* season[] = { "spring","summer","fall","winter" };

struct Date { int d,m,y; };
int day(Date* p) { return p->d; }
double sqrt(double);
template<class T> T abs(T a) { return a<0 ? -a : a; }

typedef complex<short> Point;
struct User;
enum Beer { Carlsberg,Tuborg,Thor };
namespace NS { int a; }

As can be seen from these examples,a declaration can do more than simply
associate a type with a name. Most of these declarations are also definitions;
that is,they also define an entity for the name to which they refer. For ch,
that entity is the appropriate amount of memory to be used as a variable – that
memory will be allocated. For day it is the specified function. For the
constant pi,it is the value 3.1415926535897932385. For Date,that entity is
a new type. For Point it is the type complex so that Point becomes a
synonym for complex
. Of the declarations above,only these are not also
definitions:

double sqrt(double);
extern int error_number;
struct User;
typedef complex<short> Point <-- WTF;

大胆的句子是不是与下面给出的列表冲突了? typedef只是一个声明还是一个定义?这是书中的错误吗?

解决方法

虽然我对此完全感到困惑.标准很清楚. Typedef只是一个声明.不定义.

3.1-2

A declaration is a definition unless
it declares a function without
specifying the function’s body (8.4),
it contains the extern specifier
(7.1.1) or a linkage-specification24)
(7.5) and neither an initializer nor a
function-body,it declares a static
data member in a class declaration
(9.4),it is a class name declaration
(9.1),or it is a typedef
declaration (7.1.3)
,a
using-declaration (7.3.3),or a
using-directive (7.3.4).

编辑:哦,我才明白为什么.您可以键入一个声明,因此typedef必须是一个声明本身.

猜你在找的C&C++相关文章