1、构造函数的回顾
- 关于构造函数
— 类的构造函数用于对象的初始化
— 构造函数与类同名并且没有返回值
— 构造函数在对象定义时自动被调用 - 问题
1、如何判断构造函数的执行结果?
2、在构造函数中执行 return 语句会发生什么?
3、构造函数执行结束是否意味着对象构造成功?
#include <stdio.h>
class Test
{
private:
int mi;
int mj;
public:
Test(int i,int j)
{
mi = i;
return;
mj = j;
}
~Test()
{
}
int getI()
{
return mi;
}
int getJ()
{
return mj;
}
};
int main()
{
Test t1(1, 2);
printf("t1.i = %d\n", t1.getI());
printf("t1.j = %d\n", t1.getJ());
return 0;
}
对象的诞生与构造函数的执行结果没有关系,即便构造函数执行结果不正确,对象依旧诞生了,只不过成员变量的初始化出现问题。
1、如何判断构造函数的执行结果?
无法判断,就像上一个程序,构造函数已经出现问题了,编译依旧正确
2、在构造函数中执行 return 语句会发生什么?
构造函数立即返回,返回之后意味着构造函数执行结束
3、构造函数执行结束是否意味着对象构造成功?
构造函数执行结束不一定对象构造成功
解决方法:添加一个m_status
#include <stdio.h>
class Test
{
private:
int mi;
int mj;
bool m_status;
public:
Test(int i,int j):m_status(false)
{
mi = i;
return;
mj = j;
m_status = true;
}
int getI()
{
return mi;
}
int getJ()
{
return mj;
}
bool status()
{
return m_status;
}
};
int main()
{
Test t1(1, 2);
if (t1.status())
{
printf("t1.i = %d\n", t1.getI());
printf("t1.j = %d\n", t1.getJ());
}
return 0;
}
没有打印任何东西,这确实是一种解决方案,只不过不太方便,要手工调用status这个函数。
2、你该知道的真相
- 构造函数
— 只提供自动初始化成员变量的机会
— 不能保证初始化逻辑一定成功
— 执行return 语句后构造函数立即结束
构造函数能决定的只是对象的初始状态,而不是对象的诞生。 - 半成品对象的概念
— 初始化操作不能按照预期完成而得到的对象
— 半成品对象是合法的C++对象,也是Bug的重要来源。
3、二阶构造
- 工程开发中的构造过程可分为
— 资源无关的初始化操作
不可能出现异常情况的操作
— 需要使用系统资源的操作
可能出现的异常情况,如:内存申请,访问文件
二阶构造示例一:
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;
}
二阶构造的构造函数是在 private 里面,于是当我们定义一个对象,不能直接定义,外部函数无法访问private 里面的成员函数,我们不能直接通过类名来定义一个对象了,要设立一个对象创建函数NewInstance( )。对象创建函数的第一步就是在堆空间里面创建一个对象,在创建对象的时候,构造函数肯定会被调用,这时候执行的就是第一阶段的构造,定义判断当前对象有没有创建出来。第二阶段的构造就是靠construct()函数。
内部可以访问private 的构造函数
#include <stdio.h>
class TwoPhaseCons
{
private:
TwoPhaseCons()
{
}
bool construct()
{
return true;
}
public:
static TwoPhaseCons* NewInatance();
};
TwoPhaseCons* TwoPhaseCons::NewInatance()
{
TwoPhaseCons* ret = new TwoPhaseCons;
if (!(ret && ret->construct()))
{
delete ret;
ret = NULL;
}
return ret;
}
int main()
{
TwoPhaseCons obj;
return 0;
}
我们应该访问public 里面的NewInstance函数,根据它在堆上生成对象
#include <stdio.h>
class TwoPhaseCons
{
private:
TwoPhaseCons()
{
}
bool construct()
{
return true;
}
public:
static TwoPhaseCons* NewInatance();
};
TwoPhaseCons* TwoPhaseCons::NewInatance()
{
TwoPhaseCons* ret = new TwoPhaseCons;
if (!(ret && ret->construct()))
{
delete ret;
ret = NULL;
}
return ret;
}
int main()
{
TwoPhaseCons* pt = TwoPhaseCons::NewInatance();
printf("pt = %p\n", pt);
return 0;
}
如果一旦第二阶段构造函数内存出错,返回false,看看这个类会有什么输出?
#include <stdio.h>
class TwoPhaseCons
{
private:
TwoPhaseCons()
{
}
bool construct()
{
return false;
}
public:
static TwoPhaseCons* NewInatance();
};
TwoPhaseCons* TwoPhaseCons::NewInatance()
{
TwoPhaseCons* ret = new TwoPhaseCons;
if (!(ret && ret->construct()))
{
delete ret;
ret = NULL;
}
return ret;
}
int main()
{
TwoPhaseCons* pt = TwoPhaseCons::NewInatance();
printf("pt = %p\n", pt);
return 0;
}
总程序:二阶构造改编IntArray
IntArray.h
#ifndef _INTARRAY_H_
#define _INTARRAY_H_
#pragma once
class IntArray
{
private:
int* m_pointer;
int m_length;
IntArray(int len);
bool construct();
public:
static IntArray* NewInstance(int length);
int length();
bool set(int index,int value);
bool get(int index, int& value);
~IntArray();
};
#endif
IntArray.cpp
#include "IntArray.h"
IntArray::IntArray(int len)
{
m_length = len;
}
bool IntArray::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;
}
int IntArray::length()
{
return m_length;
}
IntArray* IntArray::NewInstance(int length)
{
IntArray* ret = new IntArray(length);
if (!(ret&&ret->construct()))
{
delete ret;
ret = 0;
}
return ret;
}
bool IntArray::set(int index, int value)
{
bool ret = (index >= 0) && (index < m_length);
if (ret)
{
m_pointer[index] = value;
}
return ret;
}
bool IntArray::get(int index, int& value)
{
bool ret = (index >= 0) && (index < m_length);
if (ret)
{
value = m_pointer[index];
}
return ret;
}
IntArray::~IntArray()
{
delete []m_pointer;
}
main.cpp
#include <stdio.h>
#include "IntArray.h"
int main()
{
IntArray* a = IntArray::NewInstance(5);
for (int i = 0; i < a->length(); i++)
{
a->set(i, i + 1);
}
for (int i = 0; i < a->length(); i++)
{
int value = 0;
if (a->get(i, value))
{
printf("a[%d] = %d\n", i, value);
}
}
delete a;
return 0;
}
用了二阶构造之后,对象只能在堆空间中产生,不能在栈上面产生,这样好吗?
在工程中,对象往往是很大的,对象往往不适合放在栈上,都应该放到堆空间里面
小结:
- 构造函数只能决定对象的初始化状态
- 构造函数中初始化操作的失败不影响对象的诞生
- 初始化不完全的半成品对象是Bug的重要来源
- 二阶构造认为的将初始化过程分为两部分
- 二阶构造能够确保创建的对象都是完整初始化的。