如何用静态对象编写库? [英] How to write a library with a static object?

查看:64
本文介绍了如何用静态对象编写库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个关于编写图书馆的问题。让我通过以下方式描述

库:


*有一个A类可供用户使用

*有一个B级用于severel''水下作业''

*有一个列表存储B级物品


有几个问题我不确定


*最好将所有内容放在hpp文件中,所以没有cpp文件
使用
(#include是你需要使用lib)?

*如何获得列表实例?我的图书馆应该始终可以获得
。在hpp文件中使用静态

成员''list< class_Bmy_list;''为C类提供我的列表是明智的吗?还是有一个

这样做更好的方法...


问候,


Jeroen

推荐答案

Jeroen写道:
Jeroen wrote:

大家好,


我有一个关于写一个图书馆的问题。让我通过以下方式描述

库:


*有一个A类可供用户使用

*有一个B级用于severel''水下作业''

*有一个列表存储B级物品


有几个问题我不确定


*最好将所有内容放在hpp文件中,所以没有cpp文件

使用(#include是你需要使用lib)?
Hi all,

I''ve got a question about writing a library. Let me characterize that
library by the following:

* there is a class A which is available to the user
* there is a class B that is used in severel ''underwater operations''
* there is a list which stores objects of class B

There are several issues I''m not sure about:

* Is it best practice to put everything in hpp-files, so no cpp file is
used (#include is al you need to use the lib)?



这是可能的。主要问题我想如果你有很多代码,你的用户会增加编译时间


It''s a possibility. Main issue I guess would be increased compile time
for your users if you have a lot of code.


*我如何得到一个列表的实例?我的图书馆应该始终可以获得
。在hpp文件中使用静态

成员''list< class_Bmy_list;''为C类提供我的列表是明智的吗?或者是否有更好的方法来实现这一目标...
* How do I get an instance of the list? It should always be available to
my library. Is it wise to have a class C in the hpp-file with static
member ''list<class_Bmy_list;'' which provides me my list? Or is there a
better way to do so...



这里的重大问题是''​​静态初始化命令惨败'' 。有没有完美的答案,但是你的列表作为全球

函数的静态成员是你能做的最好的。请参阅此处

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


>

问候,


Jeroen
>
Regards,

Jeroen



john

john


John Harrison写道:
John Harrison wrote:

Jeroen写道:
Jeroen wrote:

>大家好,

*有一个可供用户使用的A类
*有一个用于severel''的B类水下作业''
*有一个列出B类物品的清单

有几个问题我不确定

*最好将所有内容放在hpp文件中,所以没有使用cpp文件(#include是你需要使用lib)?
>Hi all,

I''ve got a question about writing a library. Let me characterize that
library by the following:

* there is a class A which is available to the user
* there is a class B that is used in severel ''underwater operations''
* there is a list which stores objects of class B

There are several issues I''m not sure about:

* Is it best practice to put everything in hpp-files, so no cpp file
is used (#include is al you need to use the lib)?




这是可能的。主要问题我想如果你有很多代码,你的用户会增加编译时间




It''s a possibility. Main issue I guess would be increased compile time
for your users if you have a lot of code.


> *我如何获取列表的实例?它应该始终可用于我的图书馆。在hpp文件中使用
静态成员''list< class_Bmy_list;''为C类提供我的列表是明智的吗?或者
有更好的方法吗...
>* How do I get an instance of the list? It should always be available
to my library. Is it wise to have a class C in the hpp-file with
static member ''list<class_Bmy_list;'' which provides me my list? Or
is there a better way to do so...




这里的重大问题是''​​静态初始化订单惨败'' 。

没有完美的答案,但你的列表作为

全局函数的静态成员是你能做的最好的。请参阅此处

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


>>
问候,

Jeroen
>>
Regards,

Jeroen




john



john



OK ,thanx。非常有帮助阅读链接。让我尝试更好的

构造:如果我回到我的代码(还要写......),那么我有:


* A类用户可以使用

* B级用于''水下'',但仅限于用户确实使用A类的b $ b / b

* B类对象的列表(只有在用户确认的情况下才能访问
A类的东西...)


所以也许我如果我把列表作为A级的静态成员,我会安全吗?


B级{

int blah;

} ;


A级{

私人:

静态列表< Bmy_list;

static void put_B_in_the_list (const B& b);

静态B& find_B_in_the_list(int blah);


int more_blah;

};


鉴于列表是只有当图书馆的用户使用A类时才能访问,这应该可以防止链接中描述的init惨败

你给了什么?


再次使用Thanx,


Jeroen

OK, thanx. Very helpfull to read the link. Let me try a better
construction: if I go back to my code (yet to write...), then I have:

* class A that''s available to the user
* class B that is used ''underwater'', but only if the user does
something with class A
* a list for objects of class B (only accessed if the user does
something with class A...)

So maybe I am safe if I put the list as a static member of class A?

class B {
int blah;
};

class A {
private:
static list<Bmy_list;
static void put_B_in_the_list(const B& b);
static B& find_B_in_the_list(int blah);

int more_blah;
};

Given the fact that the list is only accessed if the user of the library
uses class A, this should prevent the init fiasco described in the link
you gave?

Thanx again,

Jeroen


Jeroen写道:
Jeroen wrote:

John Harrison写道:
John Harrison wrote:

> Jeroen写道:
>Jeroen wrote:

>>大家好,

我对编写图书馆有疑问。让我通过以下方式描述该库:

*有一个可供用户使用的A类
*有一个用于severel''的B类水下作业''
*有一个列出B类物品的清单

有几个问题我不确定

*最好将所有内容放在hpp文件中,所以没有使用cpp文件(#include是你需要使用lib)?
>>Hi all,

I''ve got a question about writing a library. Let me characterize that
library by the following:

* there is a class A which is available to the user
* there is a class B that is used in severel ''underwater operations''
* there is a list which stores objects of class B

There are several issues I''m not sure about:

* Is it best practice to put everything in hpp-files, so no cpp file
is used (#include is al you need to use the lib)?



这是可能的。如果您有很多代码,我想主要问题是为您的用户增加编译时间。


It''s a possibility. Main issue I guess would be increased compile time
for your users if you have a lot of code.


>> *我如何获得列表的实例?它应该始终可用于我的图书馆。在hpp文件中使用
静态成员''list< class_Bmy_list;''为C类提供我的列表是明智的吗?或者
有更好的方法吗...
>>* How do I get an instance of the list? It should always be available
to my library. Is it wise to have a class C in the hpp-file with
static member ''list<class_Bmy_list;'' which provides me my list? Or
is there a better way to do so...



这里最大的问题是''​​静态初始化命令惨败''。
没有完美的答案,但将您的列表作为
全局函数的静态成员是您可以做的最好的。请参阅此处

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


>>> <问候,

Jeroen
>>>
Regards,

Jeroen



john


john




好,thanx。非常有帮助阅读链接。让我尝试更好的

构造:如果我回到我的代码(还要写......),那么我有:


* A类用户可以使用

* B级用于''水下'',但仅限于用户确实使用A类的b $ b / b

* B类对象的列表(只有在用户确认的情况下才能访问
A类的东西...)


所以也许我如果我把列表作为A级的静态成员,我会安全吗?


B级{

int blah;

} ;


A级{

私人:

静态列表< Bmy_list;

static void put_B_in_the_list (const B& b);

静态B& find_B_in_the_list(int blah);


int more_blah;

};


鉴于列表是只有当图书馆的用户使用A类时才能访问,这应该可以防止链接中描述的init惨败

你给了什么?


再次使用Thanx,


Jeroen



OK, thanx. Very helpfull to read the link. Let me try a better
construction: if I go back to my code (yet to write...), then I have:

* class A that''s available to the user
* class B that is used ''underwater'', but only if the user does
something with class A
* a list for objects of class B (only accessed if the user does
something with class A...)

So maybe I am safe if I put the list as a static member of class A?

class B {
int blah;
};

class A {
private:
static list<Bmy_list;
static void put_B_in_the_list(const B& b);
static B& find_B_in_the_list(int blah);

int more_blah;
};

Given the fact that the list is only accessed if the user of the library
uses class A, this should prevent the init fiasco described in the link
you gave?

Thanx again,

Jeroen



不,如果您的用户试图在构建a时使用A类全局

对象无法保证my_list将被构建。

只有将my_list作为静态放在全局中才能获得保证

功能如常见问题所示。


john

No, if your user attempts to use class A in the construction of a global
object there is no guarantee that my_list will have been constructed.
You only get that guarantee by putting my_list as a static in a global
function as shown in the FAQ.

john


这篇关于如何用静态对象编写库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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