21-对象的构造顺序

注:博客中内容主要来自《狄泰软件学院》,博客仅当私人笔记使用。

测试环境:Ubuntu 10.10

GCC版本:4.4.5

 

一、问题

        C++中的类可以定义多个对象,那么对象构造的顺序是怎样的

 

二、对象的构造顺序一

1)对于局部对象

  - 当程序执行流到达对象的定义语句时进行构造

 

2)下面程序中的对象构造顺序是什么?

int i = 0;
Test a1 = i;

while(i < 3)
   Test a2 = ++i;

if(i < 4)
   Test a = a1;
else
   Test a(100);

 

实例分析
局部对象的构造顺序
21-1.cpp	
#include <stdio.h>

class Test
{
private:
    int mi;
public:
    Test(int i)
    {
        mi = i;
        printf("Test(int i): %d\n", mi);
    }
    Test(const Test& obj)
    {
        mi = obj.mi;
        printf("Test(const Test& obj): %d\n", mi);
    }
};

int main()
{
    int i = 0;
    Test a1 = i;	//Test(int):0
        
    while( i < 3 )
    {
        Test a2 = ++i;	//Test(int i):1,2,3
    }
        
    if( i < 4 )
    {
        Test a = a1;    //Test(const Test& obj):0
    }
    else
    {
        Test a(100);
    }

    return 0;
}

操作:

1) g++ 21-1.cpp -o 21-1.out编译正确,打印结果:

Test(int i): 0
Test(int i): 1    
Test(int i): 2    
Test(int i): 3    
Test(const Test& obj): 0    

 

特殊:error.cpp
#include <stdio.h>

class Test
{
private:
    int mi;
public:
    Test(int i)
    {
        mi = i;
        printf("Test(int i): %d\n", mi);
    }
    Test(const Test& obj)
    {
        mi = obj.mi;
        printf("Test(const Test& obj): %d\n", mi);
    }
    int getMi()
    {
        return mi;
    }
};

int main()
{
    int i = 0;
    Test a1 = i; 		//Test(int i): 0
        
    while( i < 3 )
    {
        Test a2 = ++i;	//Test(int i): 1, 2, 3
    }
	
goto End;       
    Test a(100);  	
End:
    printf("a.mi = %d\n", a.getMi());
    return 0;
}

操作:

1) g++ error.cpp -o error.out编译错误:

error.cpp:38:1: error: jump to label 'End'
    End:
error.cpp:36:6: error: from here
    goto End;
error.cpp:37:7: error: crosses initialization of 'Test a'
    Test a(100);
错误:跳过了初始化'Test a'

 

三、对象的构造顺序二

1)对于堆对象

     - 当程序执行流到达new语句时创建对象

     - 使用new创建对象将自动触发构造函数的调用

 

2)下面程序中的对象构造顺序是什么?

int i = 0;
Test* a1 = new Test(i);

while( ++i < 10)
   if(i % 2)
      new Test(i);

if(i < 4)
   new Test(*a1);
else
   new Test(100);

 

编程实验
堆对象的构造顺序
21-2.cpp

#include <stdio.h>

class Test
{
private:
    int mi;
public:
    Test(int i)
    {
        mi = i;
        printf("Test(int i): %d\n", mi);
    }
    Test(const Test& obj)
    {
        mi = obj.mi;
        printf("Test(const Test& obj): %d\n", mi);
    }
    int getMi()
    {
        return mi;
    }
};

int main()
{
    int i = 0;
    Test* a1 = new Test(i); // Test(int i): 0
        
    while( ++i < 10 )
        if( i % 2 ) //1-10中的奇数对象
            new Test(i); // Test(int i): 1, 3, 5, 7, 9
        
    if( i < 4 )
        new Test(*a1);
    else
        new Test(100); // Test(int i): 100
        
    return 0;
}

操作:

1) g++ 21-2.cpp -o 21-2.out编译正确,打印结果:

Test(int i):0
Test(int i):1
Test(int i):3
Test(int i):5
Test(int i):7
Test(int i):9
Test(int i):100

 

四、对象的构造顺序三

1)对象全局对象

     - 对象的构造顺序是不确定的

     - 不同的编译器使用不同的规则确定构造顺序

实例分析
全局对象的构造顺序
21-3.cpp

#include "test.h"

Test t4("t4");

int main()
{
    Test t5("t5");
}


t1.cpp
#include "test.h"

Test t1("t1");

t2.cpp
#include "test.h"

Test t2("t2");

t3.cpp
#include "test.h"

Test t3("t3");

test.h
#ifndef _TEST_H_
#define _TEST_H_

#include <stdio.h>

class Test
{
public:
    Test(const char* s)
    {
        printf("%s\n", s);
    }
};

#endif

操作:

1) g++ t1.cpp t2.cpp t3.cpp 21-3.cpp -o 21-3.out编译正常,打印结果:

t1
t2
t3
t4
t5

 

小结

1)局部对象的构造顺序依赖于程序的执行流

2)堆对象的构造顺序依赖于 new 的使用顺序

3)全局对象的构造顺序是不确定的

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值