C++, is it possible to call a constructor directly, without new?

后端 未结 9 640
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 10:40

Can I call constructor explicitly, without using new, if I already have a memory for object?

class Object1{
    char *str;
public:
    Object1(c         


        
9条回答
  •  误落风尘
    2020-12-02 11:03

    Yes, using placement new - as above, but you might consider having a second factory class to manage the storage, even if it means copying an object. memcpy() is generally cheap for small objects.

提交回复
热议问题