静态/非静态数据成员声明/定义 [英] static/nonstatic data member declaration/definition

查看:119
本文介绍了静态/非静态数据成员声明/定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,如果你写了


class X {

int y;

static int z;

};


然后你已定义(和声明)X和y,但你只宣布了

(而不是定义)z。如果你想真正定义z,你还要

需要添加


int X :: z;


有人能告诉我这个语言被设计成这样的原因吗?如果z以与y相同的方式定义z似乎会更简单,所以可能有一些很好的理由。

My understanding is that if you write

class X {
int y;
static int z;
};

then you''ve defined (and declared) X and y, but you have only declared
(and not defined) z. If you''d like to actually define z, you also
need to add

int X::z;

Can anybody tell me the reason that the language was designed to be
like this? It seems it would be simpler if z were defined in the same
way as y, so presumably there''s some good reason.

推荐答案

Jeffrey写道:
Jeffrey wrote:

我的理解是,如果你写了


class X {

int y;

static int z;

};


然后你定义了(和声明)X和y,但你只声明了

(并且未定义)z。如果你想真正定义z,你还要

需要添加


int X :: z;


有人能告诉我这个语言被设计成这样的原因吗?如果z以与y相同的方式定义,那么似乎会更简单,所以可能有一些很好的理由。
My understanding is that if you write

class X {
int y;
static int z;
};

then you''ve defined (and declared) X and y, but you have only declared
(and not defined) z. If you''d like to actually define z, you also
need to add

int X::z;

Can anybody tell me the reason that the language was designed to be
like this? It seems it would be simpler if z were defined in the same
way as y, so presumably there''s some good reason.



因为您只希望定义在一个翻译单元中,或者

您将收到链接错误。另一方面,声明应该是每个需要访问它们的翻译单元。

Because you only want the definition to be in one translation unit, or
you''ll get link errors. The declarations, on the other hand, should be
in every translation unit that needs access to them.


Jeff Schwab写道:
Jeff Schwab wrote:

Jeffrey写道:
Jeffrey wrote:

>我的理解是,如果你写了

类X {
int y;
static int z;
};

然后你已定义(和声明)X和y,但你只声明了
(未定义)z。如果你想真正定义z,你还需要添加

int X :: z;

任何人都可以告诉我原因是什么语言被设计成这样吗?如果z以与y相同的方式定义,似乎会更简单,所以可能有一些很好的理由。
>My understanding is that if you write

class X {
int y;
static int z;
};

then you''ve defined (and declared) X and y, but you have only declared
(and not defined) z. If you''d like to actually define z, you also
need to add

int X::z;

Can anybody tell me the reason that the language was designed to be
like this? It seems it would be simpler if z were defined in the same
way as y, so presumably there''s some good reason.



因为你只希望定义在一个翻译单元中,或者

你会得到链接错误。另一方面,声明应该在每个需要访问它们的翻译单元中为


Because you only want the definition to be in one translation unit, or
you''ll get link errors. The declarations, on the other hand, should be
in every translation unit that needs access to them.



顺便说一下,类模板有一个循环漏洞。静态成员变量

类模板(不包括显式特化)可以直接在头文件中生成

,以及相应的其余部分

模板定义。

Btw, there''s a loop-hole for class templates. Static member variables
of class templates (not including explicit specializations) can live
right up in the header file, along with the rest of the corresponding
template definition.


Jeffrey写道:
Jeffrey wrote:

我的理解是,如果你写了


class X {

int y;

static int z;

};


然后你已定义(和声明)X和y,但你只声明了

(并且没有定义)z。如果你想真正定义z,你还要

需要添加


int X :: z;


有人能告诉我这个语言被设计成这样的原因吗?
My understanding is that if you write

class X {
int y;
static int z;
};

then you''ve defined (and declared) X and y, but you have only declared
(and not defined) z. If you''d like to actually define z, you also
need to add

int X::z;

Can anybody tell me the reason that the language was designed to be
like this?



好​​吧,无论何时实例化该类,都会得到成员y。但是z是

应该完全存在,而不是每个对象都存在一次。

Well, whenever you instantiate the class, you get the member y. But z is
supposed to exist exactly noce, not once for every object.


这篇关于静态/非静态数据成员声明/定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆