C + + language Learning (vi)--second-order tectonic mode one, the problem of the constructor function
Problems with constructors:
A, the constructor only provides an opportunity to initialize member variables automatically
B, there is no guarantee that the initialization logic will succeed, such as requesting system resources may fail
C. The constructor ends immediately after executing the return statement
The constructor creates objects that may be semi-finished objects, which are legitimate objects, but one of the sources of program bugs. Therefore, the second-order tectonic model is used in the actual engineering development process.
A brief introduction to the two or two-order tectonic mode 1.
Because of the potential problems of constructors, the construction of class objects in real engineering development is as follows:
A, resource-independent initialization operation
Resource-independent initialization operations typically do not show exceptions
B, System resource-related operations
For system resource-related operations such as heap space requests, file access may fail.
The flow of the second order construction mode is as follows:
The second-order construction mode ensures that the created objects are fully initialized. Because class objects occupy a large amount of storage space in engineering practice, they generally need to be allocated in heap space, so the second-order construction mode constructs objects in a way that assigns objects to stacks and global data extents in the constructor, preserving only the construction of objects created in the heap space.
2, second-order construction mode Example
Example code for second-order construction mode:
#include <stdio.h>class TwoPhaseCons {private: TwoPhaseCons() // 第一阶段构造函数 { } bool construct() // 第二阶段构造函数 { return true; }public: static TwoPhaseCons* NewInstance(); // 对象创建函数};TwoPhaseCons* TwoPhaseCons::NewInstance() { TwoPhaseCons* ret = new TwoPhaseCons(); // 若第二阶段构造失败,返回 NULL if( !(ret && ret->construct()) ) { delete ret; ret = NULL; } return ret;}int main(){ TwoPhaseCons* obj = TwoPhaseCons::NewInstance(); printf("obj = %p\n", obj); delete obj; return 0;}
3. Second-order structural function application
#include <iostream>using namespace Std;class intarray{private:intarray (int len) {m_length = Len; } intarray (const intarray& obj); BOOL Construct () {BOOL ret = true; M_pointer = new Int[m_length]; if (M_pointer) {for (int i=0; i<m_length; i++) {m_pointer[i] = 0; }} else {ret = false; } return ret; }public:static intarray* newinstance (int length) {intarray* ret = new Intarray (length); If the resource request fails if (! ( RET && ret->construct ())) {DELETE ret; ret = 0; } return ret; } int Length () {return m_length; } bool Get (int index, int& value) {BOOL ret = (0 <= index) && (Index < length ()); if (ret) {value = M_pointer[index]; } return ret; } bool Set (int index, int value) {BOOL ret = (0 <= index) && (Index < length ()); if (ret) {M_pointer[index] = value; } return ret; } ~intarray () {delete [] m_pointer; }private:int m_length; Int* M_pointer;}; int main (int argc, char *argv[]) {intarray* array = intarray::newinstance (5); cout << array->length () << Endl; return 0;}
C + + language Learning (vi)-second-order construction mode