对象创建的特殊方式?

Zie*_*ezi 2 c++ oop malloc templates new-operator

以下是实现创建对象的类的示例:

template <class T>
struct MallocCreator {
    static T* Create() {
        void* buf = std::malloc(sizeof(T));
        if (!buf) return 0;
        return new(buf) T;
    }
};
Run Code Online (Sandbox Code Playgroud)

通过使用mallocnew操作员.

new上述语法的作用是什么,究竟是什么?

Chr*_*ckl 5

它是"放置新的".你的代码确实是一个大小创建原始内存Tmalloc再建立一个新的T在原始内存.