初始化静态类成员 [英] Initializing static class member

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

问题描述

大家好,


我正在编写一个包含一些实用程序类的Windows DLL。

我的一个类是Singleton,因此包含一些静态成员。

我正在使用VC6并静态链接到DLL。


我的问题是初始化的正确(和最佳方式)在哪里

这些变量?如果我在类CPP中初始化它们(在DLL中)

然后当我尝试将我的项目与LIB链接时,我会收到链接错误,

我不会我想让用户初始化那些变量。


---- DLL mydll.dll:

classA.h:


类DLL_EXPORT A {

static int x;

}


classA.cpp

我想写:


int A :: x = 0;

----


谢谢,

Avi

解决方案

Avi Uziel写道:

大家好,我正在编写一个包含一些实用程序类的Windows DLL。
我的一个类是Singleton,因此包含一些静态成员。
我正在使用VC6并静态链接到DLL。



[SNIP]


您的问题是Windows(DLL)特定的。请发布到Windows

编程新闻组。在此找到一个:

http:// www.slack.net/~shiva/welcome.txt


或常见问题解答:

http://www.parashift.com/c++-faq-lit ... t.html#faq-5.9


-

Attila aka WW



" Attila Feher" <在********** @ lmf.ericsson.se>在消息中写道

news:bk ********** @ newstree.wise.edt.ericsson.se ...

Avi Uziel写道:< blockquote class =post_quotes>大家好,

我正在编写一个包含一些实用程序类的Windows DLL。
我的一个类是Singleton,因此包含一些静态成员。<我正在使用VC6并静态链接到DLL。 [SNIP]

你的问题是Windows(DLL)特定的。




他的实际问题不是。

我的问题是初始化这些变量的正确(和最好的方法)在哪里?
classA.cpp
我想写:
int A :: x = 0;




这是正确的方法。如果这不起作用,他还有其他一些问题。


jeffc写道:

" Attila Feher" <在********** @ lmf.ericsson.se>在消息中写道
新闻:bk ********** @ newstree.wise.edt.ericsson.se ...

Avi Uziel写道:

大家好,

我正在编写一个包含一些实用程序类的Windows DLL。
我的一个类是Singleton,因此包含一些静态
成员。我正在使用VC6并静态链接到DLL。


[SNIP]

您的问题是Windows(DLL)特定的。


<他的实际问题不是。




确实如此。阅读帖子。

我的问题是初始化这些变量的正确(和最佳方式)在哪里?
classA .cpp
我想写:
int A :: x = 0;



这是正确的方法。如果那不起作用,他还有其他一些问题。




OP写道:

8< == =

如果我在类CPP中初始化它们(在DLL中)

那么当我尝试将项目与LIB链接时,我会收到链接错误,

,我不想让用户初始化这些变量。


===> 8


他有问题。它似乎与DLL有关。


-

WW aka Attila


Hi All,

I''m writing a Windows DLL which contain some utility classes.
One of my classes is Singleton, therefore contain some static members.
I''m using VC6 and linking to the DLL statically.

My question is where is the correct (and best way) to initialize
theses variables? If I initialize them in the class CPP (in the DLL)
then I get linkage error when I try to link my project with the LIB,
and I don''t want to make the user initialize those variables.

---- The DLL mydll.dll :
classA.h:

class DLL_EXPORT A{
static int x;
}

classA.cpp
I would like to write:

int A::x = 0;
----

Thanks,
Avi

解决方案

Avi Uziel wrote:

Hi All,

I''m writing a Windows DLL which contain some utility classes.
One of my classes is Singleton, therefore contain some static members.
I''m using VC6 and linking to the DLL statically.


[SNIP]

Your question is Windows (DLL) specific. Please post to a Windows
programming newsgroup. Find one in this:

http://www.slack.net/~shiva/welcome.txt

or in the FAQ:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

--
Attila aka WW



"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bk**********@newstree.wise.edt.ericsson.se...

Avi Uziel wrote:

Hi All,

I''m writing a Windows DLL which contain some utility classes.
One of my classes is Singleton, therefore contain some static members.
I''m using VC6 and linking to the DLL statically. [SNIP]

Your question is Windows (DLL) specific.



His actual question wasn''t.
My question is where is the correct (and best way) to initialize
theses variables?
classA.cpp
I would like to write:
int A::x = 0;



That is the correct way. If that doesn''t work, he has some other problem.


jeffc wrote:

"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bk**********@newstree.wise.edt.ericsson.se...

Avi Uziel wrote:

Hi All,

I''m writing a Windows DLL which contain some utility classes.
One of my classes is Singleton, therefore contain some static
members. I''m using VC6 and linking to the DLL statically.


[SNIP]

Your question is Windows (DLL) specific.



His actual question wasn''t.



It was. Read the post.

My question is where is the correct (and best way) to initialize
theses variables?
classA.cpp
I would like to write:
int A::x = 0;



That is the correct way. If that doesn''t work, he has some other
problem.



The OP wrote:
8<===
If I initialize them in the class CPP (in the DLL)
then I get linkage error when I try to link my project with the LIB,
and I don''t want to make the user initialize those variables.

===>8

He has a problem. And it very much seems to be DLL related.

--
WW aka Attila


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

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