ISO C ++中静态成员定义的基本原理 [英] Rationale for Static Member Definition in ISO C++

查看:78
本文介绍了ISO C ++中静态成员定义的基本原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我在这里有个问题。什么是ISO C ++背后的理由

静态成员定义?


* ISO C ++禁止非常数的类内定义/初始化

静态成员变量。


例如,而不是


类Fred {

public:

弗雷德();

...

私人:

static int N = 0;

};


必须定义为


int Fred :: N = 0;


*但是,静态成员函数可以在类中定义(或者,

inline)。


什么是(是)使用类内静态成员变量的问题

初始化?非常感谢任何输入和帮助。


Gary

解决方案

ziman137写道:

大家好,

我在这里有一个问题。 ISO C ++对静态成员定义的理由是什么?

* ISO C ++禁止非常量静态成员变量的类内定义/初始化。

例如,代替弗雷德{
公开:
弗雷德();
...
私人: static int N = 0;
};



何时进行初始化?一个静态成员对于任何类的实例都没有绑定




其中


静态const int N = 0;


可以在编译时初始化。


-

Ian Collins。


Ian Collins写道:

ziman137写道:


class Fred {
public :
Fred();
...
私人:
static int N = 0;
};
什么时候会进行初始化?静态成员不受任何类实例的约束。

静态const int N = 0;

可以在编译时初始化。




是的,这个理由是有道理的。然而,静态成员函数也不是绑定到类的任何实例的
。我们仍然可以合法地定义

静态成员函数,如下所示,


类Fred {

public:

Fred();

static void do(){// ...};

...

};


反过来我们可以说,按照定义

class cls

{

...

私人:

static int N = 0;

}

在课堂上,我们也可以初始化cls :: N'QUOT;作为类似全球的

成员,由标签静态识别。反正不太确定。


谢谢,

Gary

-
Ian Collins。




ziman137写道:

Ian Collins写道:

ziman137写道:

类Fred {
公共:
Fred();
...
私有:
static int N = 0;
};

初始化何时进行?静态成员不受任何类实例的约束。

静态const int N = 0;

可以在编译时初始化。



是的,这个理由是有道理的。然而,静态成员函数也不会绑定到类的任何实例。我们仍然可以合法地定义静态成员函数如下,

类Fred {
public:
Fred();
static void do(){ // ...};
...
};



代码,与任何其他代码没有什么不同,并且不需要

初始化。

反过来我们可以说,按照定义
class cls
{
...
私人:成员,由标签静态识别。反正不太确定。



某处还有一个实例。在某个地方

将在每个看到该类的编译单元中,所以你会以多个cls :: N实例结束




如果你有一个标题(在一个类之外),


int N = 0;


每个编译单元包括标题将有自己的本地

范围N.


如果你有


extern int N ;


N是一个只有一个实例和一个定义的全局。


一个静态类成员必须具有全局范围并且存在只有一次,所以

同样的规则适用于上面的extern int。这是一个有点直接使用''静态'的反击。


-

Ian Collins。


Hi all,

I have a question here. What is the rationale behind ISO C++ for
Static Member Definition?

* ISO C++ forbids in-class definition/initialization of non-constant
static member variables.

For example, instead of

class Fred {
public:
Fred();
...
private:
static int N = 0;
};

it must be defined as

int Fred::N = 0;

* However, a static member function can be defined in-class (or,
inline).

What is(are) the problem(s) with in-class static member variable
initialization? Would appreciate any input and help.

Gary

解决方案

ziman137 wrote:

Hi all,

I have a question here. What is the rationale behind ISO C++ for
Static Member Definition?

* ISO C++ forbids in-class definition/initialization of non-constant
static member variables.

For example, instead of

class Fred {
public:
Fred();
...
private:
static int N = 0;
};


When would the initialisation take place? A static member isn''t bound
to any instance of the class.

Where as

static const int N = 0;

Can be initialised at compile time.

--
Ian Collins.


Ian Collins wrote:

ziman137 wrote:


class Fred {
public:
Fred();
...
private:
static int N = 0;
};
When would the initialisation take place? A static member isn''t bound
to any instance of the class.

Where as

static const int N = 0;

Can be initialised at compile time.



Yes, this rationale makes sense. Yet, a static member function isn''t
bound to any instance of the class, either. We can still legally define
static member functions as follows,

class Fred {
public:
Fred();
static void do() { //... };
...
};

Reversely we may say that, as defined
class cls
{
...
private:
static int N = 0;
}
in the class, we could also initialize "cls::N" as a global-like
member, recognized by the tag "static". Not quite sure anyway.

Thanks,
Gary

--
Ian Collins.




ziman137 wrote:

Ian Collins wrote:

ziman137 wrote:

class Fred {
public:
Fred();
...
private:
static int N = 0;
};

When would the initialisation take place? A static member isn''t bound
to any instance of the class.

Where as

static const int N = 0;

Can be initialised at compile time.


Yes, this rationale makes sense. Yet, a static member function isn''t
bound to any instance of the class, either. We can still legally define
static member functions as follows,

class Fred {
public:
Fred();
static void do() { //... };
...
};


Code, which is no different form any other code and doesn''t require
initialisation.
Reversely we may say that, as defined
class cls
{
...
private:
static int N = 0;
}
in the class, we could also initialize "cls::N" as a global-like
member, recognized by the tag "static". Not quite sure anyway.


There still has to one an instance of it somewhere. That somewhere
would be in each compilation unit that sees the class, so you would end
up with multiple instances of cls::N.

If you have in a header (outside of a class),

int N = 0;

each compilation unit that includes the header will have its own locally
scoped N.

If you have

extern int N;

N is a global with only a single instance and a single definition.

A static class member has to have global scope and exist only once, so
the same rules apply as the extern int above. This is a somewhat
counter intuitive use of ''static''.

--
Ian Collins.


这篇关于ISO C ++中静态成员定义的基本原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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