Explicit Call to a Constructor

前端 未结 5 1838
萌比男神i
萌比男神i 2021-02-04 16:16

I know the concept that we can call the Constructor both Explicitly and Implicitly, and i have tested both the scenarios(generally till now my all purp

5条回答
  •  深忆病人
    2021-02-04 17:07

    I hate to say this, because it is so perverse, but there is an additional way to explicitly call the constructor.

    class integer
    {
       int m ,n;
     public:
       integer (int x , int y); 
    };
    integer :: integer (int x , int y )
    {
       m=x; n = y;
    }
    

    The constructor can be explicitly called on an already-constructed object.

    integer i(1,100);
    i.~integer();
    i.integer::integer(2,200);
    

    Here I've constructed (explicitly) an instance of integer. Then I've explicitly called its destructor. Then I've explicitly called the constructor again. I suppose you might use this idiom in testing. I am not aware of any place in the standard that forbids it. It works in Visual Studio 2010. I haven't tested a really wide range of compilers.

    These calls are explicit for large values of 'explicit'.

提交回复
热议问题