标题:gcc和vs08下编译结果不一样,大家能否解释下这两个程序原理?
只看楼主
grow
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-7-12
 问题点数:0 回复次数:0 
gcc和vs08下编译结果不一样,大家能否解释下这两个程序原理?
程序A:
#include<iostream>
#include<string>
using namespace std;
using std::string;

class Student
{
public:
     Student(void);
     Student(int num, string name, char sex);
     ~Student(void);
     void Display(void);
private:
     int m_num;
     string m_name;
     char m_sex;
};
 

Student::Student(void)
{
     m_num = 1001;
     m_name = "Brian";
     m_sex = 'M';
     cout << "DefaultConstructor" << endl;
}
 
Student::Student(int num, string name, char sex) : m_num(num), m_name(name), m_sex(sex)
{
     cout << "Constructor" << m_name << endl;
}
 
Student::~Student(void)
{
     cout << "Destructor" << m_name << endl;
     getchar();
}
 
void Student::Display(void)
{
     cout << "num: " << m_num << '\n';
     cout << "name: " << m_name << '\n';
     cout << "sex: " << m_sex << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
    Student stud[2] = {Student(1002, "lisa", 'F'), };
     stud[0].Display();
     stud[1].Display();
     getchar();
     return 0;
}
结果是:
Constructorlisa
Destructorlisa
DefaultConstructor
num: 1002
name: lisa
sex: F
num: 1001
name: Brian
sex: M
DestructorBrian
Destructorlisa

程序B:
#include<iostream>
using namespace std;

class A
{
public:
    A();
    A(int x);
    ~A() { cout << "destructor called" << itsnumber << endl;getchar();}
private:
    int itsnumber;
};

A::A()
{
    itsnumber=5;
    cout << "constructor called" << itsnumber<< endl;
    getchar();
}

A::A(int x)
{
    itsnumber=x;
    cout << "constructor called" << itsnumber << endl;
    getchar();
}

int _tmain(int argc, _TCHAR* argv[])
{
    A a[2]={A(8),};
    getchar();
    return 0;
}
运行结果是:
constructor called8
constructor called5
destructor called5
destructor called8
网上有人说有个对象的临时拷贝,但这样的话程序A和B是说不通的,望高人指点。

[ 本帖最后由 grow 于 2010-9-22 15:28 编辑 ]
搜索更多相关主题的帖子: 结果 编译 原理 gcc 
2010-09-22 15:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-321114-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.244836 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved