标准模板库作为对象的容器 [英] Standard Template Library as container for Objects

查看:68
本文介绍了标准模板库作为对象的容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准模板库是否能够存储完整的对象而不是指针。例如,下面的任何一个向量(a,c)

是否正常运行(注意C中的成员)。


class A {};

class B {};

class C {

stl :: vector< B> b;

};


std :: vector< A> a;

std :: vector< C> c;


我问的原因是我在库中有类似的东西

我写的。它尽可能地将对象添加到各种

容器并通过它们编制索引。然而,当对象退出

范围并取消分配他们的列表时,会抛出一个断言(我忘记了

消息,但它来自CRT新内容的深处)对于每个类

具有包含类的向量的对象。在发布模式下,所有的地狱

都破了(访问违规,无处不在)。

我知道这似乎会尖叫,不,不要使用STL容器带有

物件!但我想知道是否有任何其他解释,比如使用带有DLL的
STL,或链接错误的运行时和使用STL,或使用

不同且不兼容的运行 - 测试程序和DLL之间的时间

项目。那么,这些可能是原因,还是只是使用

指向对象的情况呢?那会包括stl :: auto_ptr模板

类吗?

当涉及各种

运行时,是否有任何规则应该遵循时间图书馆?比如什么组合会产生问题,而且当b / b $ b一般应该使用不同的库时?那些类似的细节

似乎从书中省略了,所以有没有好的参考资料

(最好是网站,我很穷,但是*真的*优秀的书籍还可以。)


谢谢(提前),


迈克


-

Michael Winter

M.Winter @ [no-spam] blueyonder.co.uk(删除[无垃圾邮件]通过电子邮件回复)

Is the Standard Template Library capable of storing complete objects rather
than pointers. For example, would either of the vectors below (a, c)
function correctly (note the member in C).

class A {};
class B {};
class C {
stl::vector<B> b;
};

std::vector<A> a;
std::vector<C> c;

The reason that I ask is that I had something similar to this in a library
that I was writing. It worked as far as I could add objects to the various
containers and index through them. However, when the objects dropped out of
scope and deallocated their lists an assertion was thrown (I forget the
message, but it was from deep within the bowels of CRT new) for each class
object that had a vector containing a class. In release mode, all hell
broke loose (access violations, everywhere).
I know that this would seem to scream, "No, don''t use STL containers with
objects!" but I was wondering if there was any other explanation, like using
STL with DLLs, or linking the wrong run-time and using STL, or using
different and incompatible run-times between the test program and the DLL
project. So, could those be the cause, or is it just a case of using
pointers to objects instead? Would that include the stl::auto_ptr template
class?
Are there any rules that should be followed when it comes to the various
run-time libraries? Such as what combinations produce problems, and when
generally should the different libraries be used? Those sorts of details
seemed to be omitted from books, so are there any good references out there
(preferably web-sites, I''m poor, but *really* excellent books are OK).

Thank you (in advance),

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply by e-mail)

推荐答案



" Michael Winter" < M.Winter @ [无垃圾邮件] blueyonder.co.uk> schreef in bericht

新闻:eE *********************** @ news-text.cableinet.net ...

"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> schreef in bericht
news:eE***********************@news-text.cableinet.net...
标准模板库是否能够存储完整的对象
而不是指针。例如,下面的任何一个向量(a,c)
是否正常运行(注意C中的成员)。


基本上,只要这些对象的类



支持值语义,在std :: containers中存储对象是安全的。您的对象基本上需要一个安全(并且

最好是高效的)

默认构造函数,复制构造函数和赋值运算符。我怀疑

,在下面显示的示例

中,你使用的是一个没有(正确)实现这些的类,而这个
是什么导致清理问题(可能会删除双重?)。


Marijn Haverbeke

A类{};
C类{
stl :: vector< B> b;
};

std :: vector< A> a;
std :: vector< C> c;

我问的原因是我在库中有类似的东西我正在写作。它尽可能地将对象添加到
各种容器并通过它们编制索引。但是,当对象退出
的范围并取消分配他们的列表时,会抛出一个断言(我忘记了
消息,但它来自CRT新内容的深处)每个类
具有包含类的向量的对象。在发布模式下,所有的地狱都崩溃了(访问违规,无处不在)。
我知道这似乎会尖叫,不,不要使用带有
对象的STL容器! "但我想知道是否有任何其他解释,如
使用STL与DLL,或链接错误的运行时和使用STL,或使用
不同和不兼容的运行时间测试程序和DLL
项目。那么,这些可能是原因,还是只是使用指向对象的指针呢?那会包括stl :: auto_ptr
模板类吗?
对于各种运行时库,是否有任何规则应该遵循?比如什么组合产生问题,何时通常应该使用不同的库?那些类似的细节
似乎从书中省略了,所以有没有好的参考文献
那里(最好是网站,我很穷,但*真的*优秀的书籍还可以)。 br />
谢谢(提前),

Mike

-
Michael Winter
M.Winter @ [no-垃圾邮件] blueyonder.co.uk(删除[无垃圾邮件]通过电子邮件回复)
Is the Standard Template Library capable of storing complete objects rather than pointers. For example, would either of the vectors below (a, c)
function correctly (note the member in C).
Basically, storing objects in std:: containers is safe as long as the class
of those objects
support value semantics. Your object will basically need a safe (and
preferably efficient)
default constructor, copy constructor and assignment operator. I suspect
that in the example
you show below you are using a class that does not (properly) implement
these, and that
is what causes the problem at cleanup (double deletes maybe?).

Marijn Haverbeke

class A {};
class B {};
class C {
stl::vector<B> b;
};

std::vector<A> a;
std::vector<C> c;

The reason that I ask is that I had something similar to this in a library
that I was writing. It worked as far as I could add objects to the various containers and index through them. However, when the objects dropped out of scope and deallocated their lists an assertion was thrown (I forget the
message, but it was from deep within the bowels of CRT new) for each class
object that had a vector containing a class. In release mode, all hell
broke loose (access violations, everywhere).
I know that this would seem to scream, "No, don''t use STL containers with
objects!" but I was wondering if there was any other explanation, like using STL with DLLs, or linking the wrong run-time and using STL, or using
different and incompatible run-times between the test program and the DLL
project. So, could those be the cause, or is it just a case of using
pointers to objects instead? Would that include the stl::auto_ptr template class?
Are there any rules that should be followed when it comes to the various
run-time libraries? Such as what combinations produce problems, and when
generally should the different libraries be used? Those sorts of details
seemed to be omitted from books, so are there any good references out there (preferably web-sites, I''m poor, but *really* excellent books are OK).

Thank you (in advance),

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply by e-mail)



Michael Winter写道:
Michael Winter wrote:
标准模板库是否能够存储完整的对象而不是指针。例如,下面
(a,c)中的任何一个向量都能正常运行(注意C中的成员)。

A类} {};
B类{};
C类{
stl :: vector< B> b;
};

std :: vector< A> a;
std :: vector< C> c;

我问的原因是我在写作的库中有类似的东西。它尽可能地为各种容器添加对象并通过它们编制索引。然而,当
对象退出范围并取消分配他们的列表时,会抛出一个断言(我忘记了这个消息,但它来自CRT新的内部深处)每个类具有包含
类的向量的对象。在发布模式下,所有的地狱都崩溃了(访问违规,无处不在)。
我知道这似乎会尖叫,不,不要使用STL容器
与对象! "但我想知道是否有任何其他的解释,比如使用带有DLL的STL,或链接错误的运行时
Is the Standard Template Library capable of storing complete objects
rather than pointers. For example, would either of the vectors below
(a, c) function correctly (note the member in C).

class A {};
class B {};
class C {
stl::vector<B> b;
};

std::vector<A> a;
std::vector<C> c;

The reason that I ask is that I had something similar to this in a
library that I was writing. It worked as far as I could add objects
to the various containers and index through them. However, when the
objects dropped out of scope and deallocated their lists an assertion
was thrown (I forget the message, but it was from deep within the
bowels of CRT new) for each class object that had a vector containing
a class. In release mode, all hell broke loose (access violations,
everywhere).
I know that this would seem to scream, "No, don''t use STL containers
with objects!" but I was wondering if there was any other
explanation, like using STL with DLLs, or linking the wrong run-time



[SNIP]

最有可能使用DLL。如果你将运行时静态链接到DLL和EXE,你会得到

单独的堆和麻烦。

Windows有一些问题(非主题)。 br />

STL容器完全能够存放完整的物品,并且当它们被移除时,可以正确地销毁它们。


-

Attila又名WW


[SNIP]

It was most probably using DLLs. There is some issue (off-topic here) on
Windows if you static-link the runtime to the DLL and the EXE you will get
separate heaps and trouble.

The STL containers are fully capable of storing complete objects and
destroying them properly when they are removed.

--
Attila aka WW


Attila Feher写道:
Attila Feher wrote:

最有可能使用DLL 。如果你将运行时静态链接到DLL和EXE,你会得到单独的堆和麻烦。

It was most probably using DLLs. There is some issue (off-topic here) on
Windows if you static-link the runtime to the DLL and the EXE you will get
separate heaps and trouble.


<在Windows上存在一些问题(这里是偏离主题的)。 br />
是的,我发现对所有DLL和EXE使用动态运行时是

最安全的做法(并生成最小的输出文件)。注意,这个

意味着如果它还没有存在,那么需要在目标机器上安装MSVCPxx.DLL。


-

Mike Smith



Yes, I find that using the dynamic runtime for all DLLs and EXEs is the
safest practice (and produces the smallest output file). Note that this
means that MSVCPxx.DLL needs to be installed on the target machine if it
is not already present.

--
Mike Smith


这篇关于标准模板库作为对象的容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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