加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – typedef是一个定义吗?

发布时间:2020-12-16 03:28:00 所属栏目:百科 来源:网络整理
导读:我真的很困惑.我正在阅读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";cons
我真的很困惑.我正在阅读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必须是一个声明本身.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读