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

后端 未结 9 642
隐瞒了意图╮
隐瞒了意图╮ 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:02

    Literally speaking, NO, you can't do it without the "new" keyword. See all the answers about placement new for the way to use the "new" keyword to call the constructor without actually allocating memory.

提交回复
热议问题