静态类成员变量 [英] static class member variables

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

问题描述

我有一个静态类成员变量如下:


struct A

{

static void Set(int i ){v = i; }

static int& Get(){return v; }

static int v;

};


int A :: v; //在cpp文件中定义A :: v

A :: v将具有外部链接,并且可执行文件中只有一个这个

变量的实例: br />

但是,让我们说我不想在cpp文件中定义变量(例如。

它是模板类和我不希望用户必须定义​​

变量。

在静态成员函数中定义它是否有任何问题

返回对变量的引用?是否总会只有一个本地静态变量的副本?b $ b?还有其他不可预见的问题吗?感谢

预付任何评论。


struct A

{

static void Set( int i){v()= i; }

static int& Get(){return v(); }

static int& v()

{

静态v;

返回v;

}

};


[见 http ://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

I have a static class member variable as follows:

struct A
{
static void Set (int i) { v = i; }
static int& Get () { return v; }
static int v;
};

int A::v; // define A::v in the cpp file
A::v will have external linkage and there will only be one instance of this
variable in the executable:

However, let''s say I don''t want to define the variable in the cpp file (eg.
it''s a template class and I don''t want users to have to define the
variable).
Is there anything wrong with defining it in a static member function which
returns a reference to the variable? Will there also always only be one copy
of the local static variable? Any other unforseen problems? Thanks in
advance for any comments.

struct A
{
static void Set (int i) { v() = i; }
static int& Get () { return v(); }
static int& v()
{
static v;
return v;
}
};

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

Eric写道:
我有一个静态类成员变量如下:
struct A
{
static void Set(int i){v = i;静态int& Get(){return v; }
static int v;
};

int A :: v; //在cpp文件中定义A :: v
A :: v将具有外部链接,并且可执行文件中只有一个这个变量的实例:

然而,让我们说我不想在cpp文件中定义变量(例如,
它是模板类,我不希望用户必须定义​​
变量)。


您可以在头文件中定义整个模板。


模板< typename X>

struct A

{

static void Set(int i){v = i; }

static int& Get(){return v; }

static int v;

};


模板< typename X>

int A :: v; //在头文件中定义A :: v


在静态成员函数中定义它是否有什么问题?
返回对变量的引用?是否也总是只有一个本地静态变量的副本?还有其他不可预见的问题吗?感谢
提前获取任何意见。

struct A
{
static void Set(int i){v()= i;静态int& Get(){return v();静态int& v()
{
静态v;
返回v;
}
};
I have a static class member variable as follows:

struct A
{
static void Set (int i) { v = i; }
static int& Get () { return v; }
static int v;
};

int A::v; // define A::v in the cpp file
A::v will have external linkage and there will only be one instance of this
variable in the executable:

However, let''s say I don''t want to define the variable in the cpp file (eg.
it''s a template class and I don''t want users to have to define the
variable).
You could define the entire template in the header file.

template <typename X>
struct A
{
static void Set (int i) { v = i; }
static int& Get () { return v; }
static int v;
};

template <typename X>
int A::v; // define A::v in the header file

Is there anything wrong with defining it in a static member function which
returns a reference to the variable? Will there also always only be one copy
of the local static variable? Any other unforseen problems? Thanks in
advance for any comments.

struct A
{
static void Set (int i) { v() = i; }
static int& Get () { return v(); }
static int& v()
{
static v;
return v;
}
};



理论上,这将是但是,有些连接器会违反只有一个副本。当你开始使用DLL时的规则。


话虽如此,这是保证

建设顺序的唯一方法,以防你''在调用main()之前重新构建许多相互关联的对象

,所以这是一个推荐的练习。


In theory, this will work as well, however, some linkers will violate
the "only one copy" rule when you start using DLL''s.

Having said that, this is the ONLY way to guarentee the order of
construction in case you''re constructing many inter-related objects
before calling main(), so it is a recomended practice.


11月17日2004 06:12:08 -0500,Eric< sh ***** @ yahoo.com>写道:
On 17 Nov 2004 06:12:08 -0500, Eric <sh*****@yahoo.com> wrote:
我有一个静态类成员变量如下:

struct A
{
static void Set(int i) {v = i;静态int& Get(){return v; }
static int v;
};

int A :: v; //在cpp文件中定义A :: v
A :: v将具有外部链接,并且在可执行文件中只有一个这个变量的实例:

然而,让我们说我不想在cpp文件中定义变量
(例如,它是模板类,我不希望用户必须定义​​
变量)。


您可以使用技巧将静态放在* .h而不会获得多个

simbol定义链接器错误 - 将静态放置在模板库中

类如下:


模板< int dummy> struct statics {static int v; };

模板< int dummy> int statics< dummy> :: v;


struct A:private statics< 1234>

{

//获取/设置与以前相同

};

在静态成员函数中定义它是否有什么问题?
返回对变量的引用?是否也总是只有本地静态变量的一个副本?还有其他任何不可预见的问题吗?
感谢
提前发表评论。

struct A
{
static void Set(int i){v() = i;静态int& Get(){return v();静态int& v()
{
静态v;
返回v;
}
};
I have a static class member variable as follows:

struct A
{
static void Set (int i) { v = i; }
static int& Get () { return v; }
static int v;
};

int A::v; // define A::v in the cpp file
A::v will have external linkage and there will only be one instance of
this variable in the executable:

However, let''s say I don''t want to define the variable in the cpp file
(eg. it''s a template class and I don''t want users to have to define the
variable).
You can use a trick to place the static in *.h without getting multiple
simbol definitions linker error - place that static in a template base
class as follows:

template<int dummy> struct statics { static int v; };
template<int dummy> int statics<dummy>::v;

struct A : private statics<1234>
{
// Get/Set are the same as before
};
Is there anything wrong with defining it in a static member function
which returns a reference to the variable? Will there also always only
be one copy of the local static variable? Any other unforseen problems?
Thanks in
advance for any comments.

struct A
{
static void Set (int i) { v() = i; }
static int& Get () { return v(); }
static int& v()
{
static v;
return v;
}
};




可能,你在这里忘记了静态关键字。现在编写的代码

返回对已经蒸发的局部变量的悬空引用。你

可以修复它:


static int& Get(){static int v;返回v; }


-

Maxim Yegorushkin


[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



Probably, you forgot static keyword here. The code as it is written now
returns a dangling reference to the already evaporated local variable. You
can fix it as:

static int& Get () { static int v; return v; }

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


" Maxim Yegorushkin" < e - ***** @ yandex.ru>在消息新闻中写道:< op ************** @ devlx007.ipcb.net> ...
"Maxim Yegorushkin" <e-*****@yandex.ru> wrote in message news:<op**************@devlx007.ipcb.net>...

你可以使用一个技巧来将静态放在* .h而不会得到多个
simbol定义链接器错误 - 将静态放在模板库中,如下所示:

template< int dummy> struct statics {static int v; };
模板< int dummy> int statics< dummy> :: v;

struct A:private statics< 1234>
{
//获取/设置与之前相同
};

You can use a trick to place the static in *.h without getting multiple
simbol definitions linker error - place that static in a template base
class as follows:

template<int dummy> struct statics { static int v; };
template<int dummy> int statics<dummy>::v;

struct A : private statics<1234>
{
// Get/Set are the same as before
};




稍微整理一下:


模板< int = 0> struct statics {static int v; };

模板< int dummy> int statics< dummy> :: v;


struct A:private statics<>

{

//获取/设置与以前相同

};



Just a slight tidy up:

template<int = 0> struct statics { static int v; };
template<int dummy> int statics<dummy>::v;

struct A : private statics<>
{
// Get/Set are the same as before
};

在静态中定义它是否有任何问题成员函数
返回对变量的引用?是否也总是只有本地静态变量的一个副本?还有其他任何不可预见的问题吗?
感谢
提前发表评论。

struct A
{
static void Set(int i){v() = i;静态int& Get(){return v();静态int& v()
{
静态v; < ------------------ missing int
return v;
}
};
Is there anything wrong with defining it in a static member function
which returns a reference to the variable? Will there also always only
be one copy of the local static variable? Any other unforseen problems?
Thanks in
advance for any comments.

struct A
{
static void Set (int i) { v() = i; }
static int& Get () { return v(); }
static int& v()
{
static v; <------------------ missing int
return v;
}
};



可能你在这里忘记了静态关键字。现在编写的代码
返回对已经蒸发的局部变量的悬空引用。您可以将其修复为:

static int& Get(){static int v;返回v; }



Probably, you forgot static keyword here. The code as it is written now
returns a dangling reference to the already evaporated local variable. You
can fix it as:

static int& Get () { static int v; return v; }



再看一遍。唯一的错误是在我标记的行上缺少int

(当然这将在较旧的编译器上编译,因为int是预标准C ++中隐含的
)。 (另外,Get应该可以通过

值返回,但这是设计点而不是错误。)

James


[见 http://www.gotw.ca/resources/clcm。 htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]


Look again. The only mistake is a missing int on the line I marked
(which of course will compile on older compilers, since the int was
implied in pre-standard C++). (Also, Get should probably return by
value, but that''s a design point rather than an error.)
James

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


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

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