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

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

    No

    Calling instance.A() or A(1) is seens as casting  'function-style cast' : illegal as right side of '.' operator
    

    Usually if a function/functionality is to needed in constructor as well as after object is construted it is placed in init() methode and used in constructor and in other place too.

    example:

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

提交回复
热议问题