The second-order tectonic mode of C + +

Source: Internet
Author: User

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 + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.