Foreword: in C + +, a bug is often caused by a failure to invoke system resources, so a bug occurs when the class calls the constructor to allocate system resources, causing the class object to be created, but only semi-finished, in order to avoid the need to use the second construction mode
Second-order tectonic mode
The source code is as follows
1 //second-order construction. CPP: The entry point that defines the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6 7 using namespacestd;8 9 classTestTen { One Private: A intD; -Test ();//first-order construction, construction of operations that do not generate a bug assignment, etc. - BOOLConstruct ();//second-order construction, construction application system resources, memory application the Public: - //static functions, ingenious in that static functions can access private members - //and no object is required to invoke - Statictest* newinstance ();//Total Constructors + }; - + intMain () A { atTest *p =test::newinstance (); - -cout << P <<Endl; - - return 0; - } in - //first-order construction, construction of operations that do not generate a bug assignment, etc. to test::test () + { - the } * $ //second-order construction, construction application system resources, memory applicationPanax Notoginseng BOOLtest::construct () - { the return 1; + } A the //Total Constructors +test*test::newinstance () - { $Test *ret =NewTest//first-order construction $ - //first-order allocation failure or second-level allocation failure - //Delete the semi-finished object and place the empty the if(! (ret&&ret->construct ())) - {Wuyi Deleteret; theRET =NULL; - } Wu - returnret; About}
The second-order tectonic mode of C + +