English 中文(简体)
是否有理由不使用:在制造物体时共享?
原标题:Is there any reason not to use std::make_shared when constructing objects?
  • 时间:2012-01-12 23:10:50
  •  标签:
  • c++

我可以不考虑任何局势。

std::shared_ptr<Object> obj(new Object("foo", 1));

倾向于

auto obj = std::make_shared<Object>("foo", 1);

后者总是会带来更好的地方性,减少记忆分散。 是否有任何情况,如果你愿意(或被迫)使用第一种形式,除非与恢复原始点的编码相联?

最佳回答

后者总是会带来更好的地方性,减少记忆分散。

页: 1 鼓励执行《公约》时使用单一拨款作为参考值和参考值,但无需查询<>。

为什么你不想使用<代码>std:make_commd? 审议了您希望由<代码>拥有的具有大量动态分配的物体的情况:共有的-ptr ,而且你知道,对这个物体的提及可能会超过对物体的有力提及(也许有许多参考文献薄弱,只有一两个短寿命的有力参考资料)。

在此情况下,<代码>std:make_commd 选择不好,假设它分配一个拥有物体和参考数的单一块块:分配的块块(大面积,记忆)在没有强烈或软弱提及物体之前不能销毁。

如果你能积极分配物体,并将点子输入<代码>std:共有-ptr的构造,那么只要没有强有力的参考资料,该物体所占用的记忆就可公布,即使参考资料仍然薄弱。

问题回答

如果你正在构造一个在物体一生中拥有多个实体的物体,那么,是,你希望尽可能使用 st:m。 某些地方可能无法从其他人那里获得电梯;例如,工厂可能退回一个原始点或一个 st子:unique_ptr,即你希望储存在一个共享的接收器上,从而防止使用电能。

标题中的问题有点不同;你可能不希望用共同的_子来对待你们的物体。 如果一生实际上为多个物体所共享,你只能使用共享的吸收器。 否则,更倾向于分拨物体或使用独一无二的吸收器。

现代民主选举学会有“最后使用”特征。 如果你使用<代码>std:make_commd来构造该物体,如果你在寻找该物体的构造者点时,民主选举学会就获得了std:make_commd>使用的地方。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签