Can you invoke an instantiated object's class constructor explicity in C++?

前端 未结 7 2277
逝去的感伤
逝去的感伤 2020-12-31 15:17

After creating a instance of a class, can we invoke the constructor explicitly? For example

class A{
    A(int a)
    {
    }
}

A instance;

instance.A(2);
         


        
7条回答
  •  长情又很酷
    2020-12-31 15:29

    I am pretty sure you can't do that. That's the whole point, constructor IS creation of an instance of the class.

    If a constructor is not called at all, or is called twice - which consequences could it have?

    What you could do of course, is extracting some constructor logic into the method, and calling that method both in the constructor and after creation of the object.

提交回复
热议问题