"静"对象和功能 [英] "static" objects and functions

查看:65
本文介绍了"静"对象和功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我了解到有五种静态对象,即


1.全局对象

2.在命名空间范围内定义的对象

3.对象声明为static而不是类

4.对象在函数内声明为静态(即本地静态

对象)

5.在文件范围内声明的对象。


我见过上面(3)和(4)的例子,有人可以请给我一些例子以及他们对其他类型的静态

对象的目的吗?


此外,我见过一个外部声明:


A级{...};

extern A a;


这是据说是静态对象a的定义。为什么这是一个

静态对象而没有静态关键字?


除静态对象外,还有静态函数。我认为一个

静态成员函数,比如


A类{

static void f();

};

意味着它不需要任何A的实例,并且可以在课堂上运作

A.这是对的吗?然后我想知道是否有任何静态非

成员函数,如果有,何时使用它好?


示例extern A a上面还提出了另一个问题。我应该把它视为一个声明,说明有一个对象a在

中定义另一个源文件,或者,我应该将其视为一个定义,因为它的

默认构造函数可能会被调用吗?


非常感谢,

Jess

解决方案


>

我了解到有五种静态对象,即


1.全局对象

2.对象在命名空间范围内定义

3.对象声明为static而不是类

4.对象在函数内声明为静态(即本地静态

对象)

5.在文件范围内声明的对象。


我看过上面(3)和(4)的例子,有人可以给出

我为其他类型的静态

对象提供了一些示例和目的?



somefile.hxx:

------------


struct A {};


extern一个object_in_global_namespace;


namespace foo {

extern一个object_in_foo_namespace;

}


struct X

{

static一个object_inside_class;

};


void function_with_local_static();


somefile.cxx:

----- -------


一个object_in_global_namespace;


namespace foo {

一个object_in_foo_namespace;

}


AX :: object_inside_class;


void function_with_local_static()

{

静态一个local_static;

// ...

}


命名空间{

A internal_linkage_object; //(文件范围对象)

}


>

此外,我''我看到了一个外在的声明:


A级{...};

extern A a;


这被认为是静态对象a的定义。为什么这是一个

静态对象而没有静态关键词?



" extern A a"是全局对象的*声明*。它说,当你链接到
时,期望在某个地方找到定义。定义,

A a,放在源文件(不是标题)内。


你太过强调了字面含义

字静态。它有一些历史包袱我不会潜入
。在现代C ++中,唯一使用静态的应该在类内部

定义和内部函数。在前一种情况下,它用于区分每个实例和每个类;
;在后一种情况下,

它用于区分自动存储和持久的

(静态)存储。


除静态对象外,还有静态函数。我认为一个

静态成员函数,比如


A类{

static void f();

};


意味着它不需要A的任何实例,并且可以在课堂上操作

A.这是对的吗?那我想知道是否有任何静态非

成员函数,如果有,何时使用它是否合适?



如果你输入静态在常规功能面前,不推荐使用

。它最初意味着内部链接,但现在已经完成了

匿名命名空间。不要那样做。再说一遍,不要读太多这个词的字面含义。


例子extern A a上面还提出了另一个问题。我应该把它视为一个声明,说明有一个对象a在另外一个源文件中定义在

中,或者,我应该将其视为一个定义,因为它的

默认构造函数可能被调用?



前者。 extern A a是宣言。它没有声明

该对象是使用默认构造函数构造的。如果它定义为
,它可以用任何构造函数构造,例如A a(44)。


< blockquote> 5月30日上午9:38,jeffjohnson_al ... @ yahoo.com写道:


你太过强调字面意思了

wordstatic。 [...]在前一种情况下,它用于区分每个实例和每个类别之间的
[...]



Argh,当我写每班级时,我的心思是红宝石模式。 (
ruby​​中的类是包含每类数据的单例对象)。当然,这个

不是C ++中的情况;静态类成员的存储是

常规静态存储。




je *************** @ yahoo.com 写道:


namespace {

A internal_linkage_object; //(文件范围对象)

}



[...]


如果你把静态放在在常规功能面前,不推荐使用

。它最初意味着内部链接,但现在已经完成了

匿名命名空间。不要那样做。再说一次,不要读太多这个词的字面含义。



这是不正确的。在未命名的命名空间中声明的名称仍然具有外部

链接。但是,没有(便携式)方式从另一个翻译单元中访问命名对象




Markus


Hello,

I learned that there are five kinds of static objects, namely

1. global objects
2. object defined in namespace scope
3. object declared static instead classes
4. objects declared static inside functions (i.e. local static
objects)
5. objects declared at file scope.

I have seen the examples of (3) and (4) above, can someone please give
me some examples and their purpose for the other types of static
objects?

Moreover, I''ve seen an extern statement:

class A{...};
extern A a;

which is said to be a definition for static object "a". Why is this a
static object without "static" keyword?

In addition to static objects, there are static functions. I think a
static member function, such as

class A{
static void f();
};
means it doesn''t require any instance of A, and can operate on class
A. Is this right? Then I''m wondering if there''s any static non-
member function, and if there is, when is it good to use it?

The example "extern A a" above also raises another question. Should I
regard it as a declaration that says there''s an object "a" defined in
another source file, or, shall I regard it as a definition, since its
default constructor may be called?

Many thanks,
Jess

解决方案

>
I learned that there are five kinds of static objects, namely

1. global objects
2. object defined in namespace scope
3. object declared static instead classes
4. objects declared static inside functions (i.e. local static
objects)
5. objects declared at file scope.

I have seen the examples of (3) and (4) above, can someone please give
me some examples and their purpose for the other types of static
objects?

somefile.hxx:
------------

struct A { } ;

extern A object_in_global_namespace ;

namespace foo {
extern A object_in_foo_namespace ;
}

struct X
{
static A object_inside_class ;
} ;

void function_with_local_static() ;

somefile.cxx:
------------

A object_in_global_namespace ;

namespace foo {
A object_in_foo_namespace ;
}

A X::object_inside_class ;

void function_with_local_static()
{
static A local_static ;
// ...
}

namespace {
A internal_linkage_object ; // ("file scope" object)
}

>
Moreover, I''ve seen an extern statement:

class A{...};
extern A a;

which is said to be a definition for static object "a". Why is this a
static object without "static" keyword?

"extern A a" is a *declaration* of a global object. It says, "when
you link, expect to find the definition somewhere." The defintion,
"A a", is placed inside a source file (not a header).

You are placing way too much emphasis on the literal meaning of the
word "static". It has some historical baggage which I will not dive
into. In modern C++, the only use of "static" should be inside class
definitions and inside functions. In the former case, it is used to
differentiate between per-instance and per-class; in the latter case,
it is used to differentiate between automatic storage and persistent
(static) storage.

In addition to static objects, there are static functions. I think a
static member function, such as

class A{
static void f();
};

means it doesn''t require any instance of A, and can operate on class
A. Is this right? Then I''m wondering if there''s any static non-
member function, and if there is, when is it good to use it?

If you put "static" in front of a regular function, that''s deprecated
usage. It originally meant internal linkage, but that''s done with
anonymous namespaces now. Don''t do that. Again, don''t read too much
into the literal meaning of the word.

The example "extern A a" above also raises another question. Should I
regard it as a declaration that says there''s an object "a" defined in
another source file, or, shall I regard it as a definition, since its
default constructor may be called?

The former. "extern A a" is a declaration. It does not declare that
the object is constructed with the default constructor. Where it is
defined, it may be constructed with any constructor, e.g., "A a(44)".


On May 30, 9:38 am, jeffjohnson_al...@yahoo.com wrote:

You are placing way too much emphasis on the literal meaning of the
word "static". [...] In the former case, it is used to differentiate
between per-instance and per-class [...]

Argh, I had my mind in ruby mode when I wrote "per-class" (classes in
ruby are singleton objects which hold per-class data). Of course this
is not the case in C++; the storage of static class members is the
regular static storage.


Hi

je***************@yahoo.com wrote:

namespace {
A internal_linkage_object ; // ("file scope" object)
}

[...]

If you put "static" in front of a regular function, that''s deprecated
usage. It originally meant internal linkage, but that''s done with
anonymous namespaces now. Don''t do that. Again, don''t read too much
into the literal meaning of the word.

That''s incorrect. Names declared in unnamed namespaces still have external
linkage. However, there is no (portable) way to access the named object
from within another translation unit.

Markus


这篇关于&QUOT;静&QUOT;对象和功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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