类变量:范围 [英] class variables: scoping

查看:53
本文介绍了类变量:范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


假设我有一些方法:


Foo :: foo(){

static int x;

int y;

/ * ... * /

}


这里变量x由类的所有实例共享,但具有

函数范围。另一方面,变量y,也有函数

范围,但每次调用方法时都会重新初始化。


现在,我想要一个具有函数作用域的变量,即其他类函数不可见的
。但是,每次我实例化一个Foo实例时都会创建一个新的这样的变量。

基本上,我希望有类似的东西:


class Foo {

/ * ... * /

私人:

/ * ... * /

int z;

/ * ... * /

};


但是,我不想让Foo里面的所有方法都看到z。我只需要
想要方法foo来看它,就像我为x做的那样只能是foo()看到的



z的值应该在相同的

实例内的不同调用中存活,但应该为每个新实例初始化,如上所述。

但它应该有方法范围,而不是类范围。


如果扩展C ++以包含这个好主意,那将是很好的。

它可以允许例如:


Foo :: foo(){

static int x;

int y;

semistatic z ;

/ * ... * /

}


其中半静态意味着:由同一个中的所有调用共享/>
实例,只在foo()中可见。


我认为C ++中缺少这个功能,非常有用。因为

如果某个类变量仅在一个方法中使用,那么我为什么要将
放在类定义中呢。这就像是因为它们被实例共享而必须制作全局的东西。

欢迎评论。


问候,


Neil

解决方案

2004年4月3日13:23:11 -0800, nz ****** @ cs.mun.ca (Neil Zanella)写道:

你好,

假设我有一些方法:
如何调用它们函数现在?你已经有一段时间......

;-)

Foo :: foo(){
static int x;
int y;
/ * ... * /
}
这里变量x由类的所有实例共享,


不是/那个/变量x,如果你指的是Foo中的那个:foo()。那是'

a块范围静态,这意味着它只能在那个

函数中被引用(并且它的值在多次调用时仍然存在。顺便说一句。 ,

它在第一次调用该函数时被初始化为零,如果你

不给它初始化器。)


但具有
功能范围。另一方面,变量y也具有函数范围,但每次调用方法时都会重新初始化。


不是您所显示的代码。没有初始化程序的自动本地人没有初始化。

现在,我想有一个具有功能范围的变量,
即不可见其他类功能。但是,每次我实例化一个Foo实例时都会创建一个新的这样的变量。


到目前为止,您已经描述了一个简单的本地自动变量。

基本上,我希望有类似的东西:

Foo {
/ * ... * /
私人:
/ * ... * /
int z;
/ * .. 。* /
};


哦男孩。

然而,我不想让Foo里面的所有方法都看到z。我只想要方法foo来看它,就像我为x做的那样只能被foo()看到。


你在foo()中实际定义的x满足了这个(但不是你在下面指定的

。)

z的值应该在同一个实例中的不同调用中存活,但是应该为每个新实例初始化,如上所述。
但它应该有方法范围,而不是类范围。


I / think /我看到了你想要的东西,但是没有原始方法可以在C ++中声明

:你想要一个实例变量(与对象的每个

实例关联的一个副本)仅在特定函数的范围内,

对吗?没有办法。你只需要通过

声明一个私人数据成员并谨慎地只在一个函数中从

访问它来进行一些自律。
如果将C ++扩展到包含这个好主意,那将是很好的。
它可以允许例如:

Foo :: foo(){
static int x;
int y;
semistatic z;
/ * ... * /
}

其中半静态意味着:所有调用共享在同一个
实例中,只在foo()中可见。


很多东西都不错。但是这不会发生。
我认为C ++中缺少这个功能并且非常有用。因为如果某个类变量仅在一个方法中使用,那么为什么我必须将它放在类定义中。


向编译器表明它必须为每个对象腾出空间

实例化。至少目前定义了C ++的方式;-)
评论欢迎。


你得到了他们。

-leor

问候,

Neil



-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html


2004年4月3日13:23:11 -0800, nz ****** @ cs.mun.ca (Neil Zanella)写道:

你好,

假设我有一些方法:
如何调用它们函数现在?你已经有一段时间......

;-)

Foo :: foo(){
static int x;
int y;
/ * ... * /
}
这里变量x由类的所有实例共享,


不是/那个/变量x,如果你指的是Foo中的那个:foo()。那是'

a块范围静态,这意味着它只能在那个

函数中被引用(并且它的值在多次调用时仍然存在。顺便说一句。 ,

它在第一次调用该函数时被初始化为零,如果你

不给它初始化器。)


但具有
功能范围。另一方面,变量y也具有函数范围,但每次调用方法时都会重新初始化。


不是您所显示的代码。没有初始化程序的自动本地人没有初始化。

现在,我想有一个具有功能范围的变量,
即不可见其他类功能。但是,每次我实例化一个Foo实例时都会创建一个新的这样的变量。


到目前为止,您已经描述了一个简单的本地自动变量。

基本上,我希望有类似的东西:

Foo {
/ * ... * /
私人:
/ * ... * /
int z;
/ * .. 。* /
};


哦男孩。

然而,我不想让Foo里面的所有方法都看到z。我只想要方法foo来看它,就像我为x做的那样只能被foo()看到。


你在foo()中实际定义的x满足了这个(但不是你在下面指定的

。)

z的值应该在同一个实例中的不同调用中存活,但是应该为每个新实例初始化,如上所述。
但它应该有方法范围,而不是类范围。


I / think /我看到了你想要的东西,但是没有原始方法可以在C ++中声明

:你想要一个实例变量(与对象的每个

实例关联的一个副本)仅在特定函数的范围内,

对吗?没有办法。你只需要通过

声明一个私人数据成员并谨慎地只在一个函数中从

访问它来进行一些自律。
如果将C ++扩展到包含这个好主意,那将是很好的。
它可以允许例如:

Foo :: foo(){
static int x;
int y;
semistatic z;
/ * ... * /
}

其中半静态意味着:所有调用共享在同一个
实例中,只在foo()中可见。


很多东西都不错。但是这不会发生。
我认为C ++中缺少这个功能并且非常有用。因为如果某个类变量仅在一个方法中使用,那么为什么我必须将它放在类定义中。


向编译器表明它必须为每个对象腾出空间

实例化。至少目前定义了C ++的方式;-)
评论欢迎。


你得到了他们。

-leor

问候,

Neil



-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html


Neil Zanella写道:

[... ]但是,我不希望Foo中的所有方法都看到z。我只想要方法foo来看它,就像我为x做的那样只能被foo()看到。

z的值应该在不同的调用之后存活在同一个实例中,但是应该为每个新实例初始化,如上所述。
但它应该有方法范围,而不是类范围。

如果C ++会很好扩展到包括这个好主意。
它可以允许例如:

Foo :: foo(){
static int x;
int y;
semistatic z;
/ * ... * /
}


如果我理解正确,你想把一个变量(状态)关联到一个

成员函数(行为)......我会说你需要一个新对象

(当然还有一个类)。你的小物件只有一种方法,

所以我想把它称为仿函数是安全的,可能会覆盖()

opertor到看起来更像是一个函数...

其中半静态意味着:在同一个实例中的所有调用共享,并且只在foo()中可见。

我认为C ++中缺少此功能并且非常有用。因为如果某个类变量仅在一个方法中使用,那么为什么我必须将它放在类定义中。这就像是因为它们被实例共享而必须使事物变得全局化。
评论欢迎。




我不是确定将这种语言包含在语言中是很常见的..

并且使用现有的

语言结构获得相同的效果并不困难。


问候,

iuli


Hello,

Suppose I have some method:

Foo::foo() {
static int x;
int y;
/* ... */
}

Here variables x is shared by all instances of the class, but has
function scope. On the other hand, variable y, also has function
scope, but is reinitialized each time the method is called.

Now, I would like to have a variable which has function scope,
that is, is not visible by other class functions. However, every
time I instantiate an instance of Foo a new such variable is created.
Essentially, I would like to have something like the equivalent of:

class Foo {
/* ... */
private:
/* ... */
int z;
/* ... */
};

However, I do not want to have all methods inside Foo see z. I only
want method foo to see it, just like I did for x which can only be
seen by foo().

The value of z should survive different calls from within the same
instance, but should be initialized for each new instance, like above.
But it should have method scope, not class scope.

It would be nice if C++ were extended to include this nice idea.
It could for example allow something like:

Foo::foo() {
static int x;
int y;
semistatic z;
/* ... */
}

Where semistatic means: shared by all invocations within the same
instance, and only visible within foo().

I think this feature is missing from C++ and is quite useful. Because
if some class variable is used only in one method, then why should I
have to place it in the class definition. It''s like having to make
things global just because they are shared by instances.
Comments welcome.

Regards,

Neil

解决方案

On 3 Apr 2004 13:23:11 -0800, nz******@cs.mun.ca (Neil Zanella) wrote:

Hello,

Suppose I have some method: How about calling them "functions" now? You''ve been around for a while...
;-)

Foo::foo() {
static int x;
int y;
/* ... */
}

Here variables x is shared by all instances of the class,
Not /that/ variable x, if you''re referring to the one in Foo:foo(). That''s
a block-scope static, which means it can only be referred to within that
function (and its value persists across multiple calls to it. By the way,
it gets initialized to zero the first time the function is called, if you
don''t give it an initializer.)

but has
function scope. On the other hand, variable y, also has function
scope, but is reinitialized each time the method is called.
Not by the code you''ve shown. Automatic locals without an initializer are
left uninitialized.

Now, I would like to have a variable which has function scope,
that is, is not visible by other class functions. However, every
time I instantiate an instance of Foo a new such variable is created.
So far you''ve described a simple local automatic variable.
Essentially, I would like to have something like the equivalent of:

class Foo {
/* ... */
private:
/* ... */
int z;
/* ... */
};
Oh boy.

However, I do not want to have all methods inside Foo see z. I only
want method foo to see it, just like I did for x which can only be
seen by foo().
The x you actually defined up there in foo() satisfies this (but not what
you go on to specify below.)

The value of z should survive different calls from within the same
instance, but should be initialized for each new instance, like above.
But it should have method scope, not class scope.
I /think/ I see what you want, but there''s no primitive way to declare that
in C++: You want an instance variable (one copy associated with each
instance of an object) that is only in scope within a particular function,
right? No can do. You''ll just have to exercise some self-discipline by
declaring a private data member and being careful to only access it from
within the one function.

It would be nice if C++ were extended to include this nice idea.
It could for example allow something like:

Foo::foo() {
static int x;
int y;
semistatic z;
/* ... */
}

Where semistatic means: shared by all invocations within the same
instance, and only visible within foo().
Lots of things would be nice. This one isn''t going to happen, though.
I think this feature is missing from C++ and is quite useful. Because
if some class variable is used only in one method, then why should I
have to place it in the class definition.
To indicate to the compiler that it has to make room for it in each object
instantiated. At least the way C++ is currently defined ;-)
It''s like having to make
things global just because they are shared by instances.
Comments welcome.
You got ''em.
-leor

Regards,

Neil



--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software''s free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html


On 3 Apr 2004 13:23:11 -0800, nz******@cs.mun.ca (Neil Zanella) wrote:

Hello,

Suppose I have some method: How about calling them "functions" now? You''ve been around for a while...
;-)

Foo::foo() {
static int x;
int y;
/* ... */
}

Here variables x is shared by all instances of the class,
Not /that/ variable x, if you''re referring to the one in Foo:foo(). That''s
a block-scope static, which means it can only be referred to within that
function (and its value persists across multiple calls to it. By the way,
it gets initialized to zero the first time the function is called, if you
don''t give it an initializer.)

but has
function scope. On the other hand, variable y, also has function
scope, but is reinitialized each time the method is called.
Not by the code you''ve shown. Automatic locals without an initializer are
left uninitialized.

Now, I would like to have a variable which has function scope,
that is, is not visible by other class functions. However, every
time I instantiate an instance of Foo a new such variable is created.
So far you''ve described a simple local automatic variable.
Essentially, I would like to have something like the equivalent of:

class Foo {
/* ... */
private:
/* ... */
int z;
/* ... */
};
Oh boy.

However, I do not want to have all methods inside Foo see z. I only
want method foo to see it, just like I did for x which can only be
seen by foo().
The x you actually defined up there in foo() satisfies this (but not what
you go on to specify below.)

The value of z should survive different calls from within the same
instance, but should be initialized for each new instance, like above.
But it should have method scope, not class scope.
I /think/ I see what you want, but there''s no primitive way to declare that
in C++: You want an instance variable (one copy associated with each
instance of an object) that is only in scope within a particular function,
right? No can do. You''ll just have to exercise some self-discipline by
declaring a private data member and being careful to only access it from
within the one function.

It would be nice if C++ were extended to include this nice idea.
It could for example allow something like:

Foo::foo() {
static int x;
int y;
semistatic z;
/* ... */
}

Where semistatic means: shared by all invocations within the same
instance, and only visible within foo().
Lots of things would be nice. This one isn''t going to happen, though.
I think this feature is missing from C++ and is quite useful. Because
if some class variable is used only in one method, then why should I
have to place it in the class definition.
To indicate to the compiler that it has to make room for it in each object
instantiated. At least the way C++ is currently defined ;-)
It''s like having to make
things global just because they are shared by instances.
Comments welcome.
You got ''em.
-leor

Regards,

Neil



--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software''s free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html


Neil Zanella wrote:

[...]However, I do not want to have all methods inside Foo see z. I only
want method foo to see it, just like I did for x which can only be
seen by foo().

The value of z should survive different calls from within the same
instance, but should be initialized for each new instance, like above.
But it should have method scope, not class scope.

It would be nice if C++ were extended to include this nice idea.
It could for example allow something like:

Foo::foo() {
static int x;
int y;
semistatic z;
/* ... */
}
If I understood correctly, you want to associate a variable (state) to a
member function (behavior)... I would say you need a new object for that
(and a class, of course). Your little object would have just one method,
so I guess it''s safe to call it a functor, maybe overwrite the "()"
opertor to look more like a function...
Where semistatic means: shared by all invocations within the same
instance, and only visible within foo().

I think this feature is missing from C++ and is quite useful. Because
if some class variable is used only in one method, then why should I
have to place it in the class definition. It''s like having to make
things global just because they are shared by instances.
Comments welcome.



I''m not sure it is that common to justify inclusion in the language..
and it''s not that difficult to get the same effect using existing
language constructs.

regards,
iuli


这篇关于类变量:范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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