mallocメモリを割り当てるために使用する必要があります。custom が必要なカスタム クラスがありますoperator=。だとしましょうA:
class A {
public:
int n;
A(int n) : n(n) {}
A& operator=(const A& other) {
n = other.n;
return *this;
}
};
私はメモリを割り当てますmalloc:
int main() {
A* a = (A*) malloc(sizeof(A));
A b(1);
//Is it safe to do this as long as I copy everything in operator=?
*a = b;
//Clean up
a->~A();
free(a);
return 0;
}
プレースメント new も使用できることはわかっています。
a = new (a) A(b);
カスタム クラスを初期化されていないメモリにコピーしても安全ですか?
ありがとう