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

前端 未结 7 2270
逝去的感伤
逝去的感伤 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:28

    No.

    Create a method for the set and call it from the constructor. This method will then also be available for later.

    class A{
        A(int a) { Set(a); }
        void Set(int a) { }
    }
    
    A instance;
    
    instance.Set(2);
    

    You'll also probably want a default value or default constructor.

提交回复
热议问题