English 中文(简体)
是否建议使用新安置办法,从一个有活力的阵营中构筑一个多目标?
原标题:Is it recommendable to use placement new when constructing an POD-object from a dynamically created array?

鉴于任何POD类型,建议采取类似行动:

any_pod* p = new any_pod[n];
for (std::size_t i = 0; i < n; ++i)
    new (&p[i].member) other_pod(whatever);
问题回答

鉴于这个问题略为主观,我永远不会提出这样的法典。 无论它是否采取不明确的行为,它都难以读到<>>>>,并将要求任何未来的维护者花更多的时间(可能很重要)来 gr清你重新做的事情。

如果您需要不同的类型能力,要么使用<代码>boost:variant,要么boost:any,视您的需要而定。

If you just want to take the data from one class and copy/assign it to another, that s what converting constructors and converting assignment operators are for.

你在已经建造的物体上建树,再次违反了销毁的语文保证,因此,我不会这样做,不管物体是裁军部。

我很想知道,你是否试图问一下什么稍有不同:

any_pod* p = reinterpret_cast<any_pod*>(malloc(n * sizeof(any_pod[n]));
for (std::size_t i = 0; i < n; ++i)
  new (&p[i].member) other_pod(whatever);

在此情况下,你不重建,你只是把建筑在一个原始记忆中。 在这种情况下,安置是适当的。 (尽管很罕见的是,你不得不照此写法典。) 在执行诸如:主人”之类的事情时,你可以做些什么。 你们必须非常小心,在最后一枚正好被拆除后释放阵列。





相关问题
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?

热门标签