使用参数构造函数模拟new []

iam*_*ind 5 c++ arrays constructor new-operator

如果我没有修改static参数构造函数中的任何变量,那么是否正确模拟new T[N] (x,y);(带参数的数组)?

template<typename T>
void* operator new [] (size_t size, const T &value)
{
  T* p = (T*) malloc(size);
  for(int i = size / sizeof(T) - 1; i >= 0; i--)
    memcpy(p + i, &value, sizeof(T));
  return p;
}
Run Code Online (Sandbox Code Playgroud)

用法是,

struct A
{
  A () {}  // default
  A (int i, int j) {} // with arguments
};

int main ()
{
  A *p = new(A(1,2)) A[10];  // instead of new A[10](1,2)
}
Run Code Online (Sandbox Code Playgroud)

seh*_*ehe 5

我建议

 std::vector<A> v(10, A(1,2));
Run Code Online (Sandbox Code Playgroud)

我意识到这并没有真正解决数组的问题.你可以用

 p = &v[0]; 
Run Code Online (Sandbox Code Playgroud)

因为该标准保证了连续存储.但是要非常小心调整矢量大小,因为它可能使p无效

我检查了boost :: array <>(它适应了C风格的数组),但它没有定义构造函数......


Kon*_*lph 4

是 \xe2\x80\x99t好的。您将对象复制到未初始化的内存中,而没有调用正确的复制语义。

\n\n

只要您\xe2\x80\x99 仅使用 POD,就可以了。但是,当使用不是 POD 的对象(例如您的A)时,您需要采取预防措施。

\n\n

除此之外,operator new不能以这种方式使用。正如Alexandre在评论中指出的那样,数组不会\xe2\x80\x99 被正确初始化,因为 C++ 将在调用你的 后调用所有元素的构造函数operator new,从而覆盖这些值:

\n\n
#include <cstdlib>\n#include <iostream>\n\ntemplate<typename T>\nvoid* operator new [] (size_t size, T value) {\n    T* p = (T*) std::malloc(size);\n    for(int i = size / sizeof(T) - 1; i >= 0; i--)\n        new(p + i) T(value);\n    return p;\n}\n\nstruct A {\n    int x;\n    A(int x) : x(x) { std::cout << "int ctor\\n"; }\n    A() : x(0) { std::cout << "default ctor\\n"; }\n    A(const A& other) : x(other.x) { std::cout << "copy ctor\\n"; }\n};\n\nint main() {\n    A *p = new(A(42)) A[2];\n    for (unsigned i = 0; i < 2; ++i)\n        std::cout << p[i].x << std::endl;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这产生:

\n\n
int ctor\ncopy ctor\ncopy ctor\ndefault ctor\ndefault ctor\n0\n0\n
Run Code Online (Sandbox Code Playgroud)\n\n

\xe2\x80\xa6 不是期望的结果。

\n

  • 不,不要在“operator new”中构造任何内容。它应该只返回一个地址,句号。 (2认同)