Calling a constructor to re-initialize object

前端 未结 12 1744
执笔经年
执笔经年 2020-12-04 15:01

is it possible to re-initialize an object of a class using its constructor?

12条回答
  •  自闭症患者
    2020-12-04 15:40

    While most answers are reinitializing an object in two steps; first, creating an initial object, and second creating another object and swapping it with the first one using placement new, this answer covers the case that you first create a pointer to an empty object and later allocate and construct it:

    class c *c_instance; // Pointer to class c
    c_instance = new c(arg1, ..., argn) // Allocate memory & call the proper constructor 
    // Use the instance e.g. c->data
    delete c_instance; // Deallocate memory & call the destructor 
    

提交回复
热议问题