相同代码不同的编译结果

QT

main.cpp

#include <iostream>
#include "testclass.h"
using namespace std;
TestClass test(void){
    TestClass t;
    return t;
}
int main()
{
    TestClass t = test();
    cout << "Hello World!" << endl;
    return 0;
}

testclass.h:

#ifndef TESTCLASS_H
#define TESTCLASS_H
class TestClass
{
public:
    TestClass();
    TestClass(const TestClass &testClass);
};
#endif // TESTCLASS_H

testclass.cpp

#include "testclass.h"
#include <iostream>
using namespace std;
TestClass::TestClass()
{
    cout<<"test class"<<endl;
}
TestClass::TestClass(const TestClass &testClass)
{
    cout<<"copy constructor"<<endl;
}

运行结果:

 

Vs

main.cpp

#include <iostream>
#include "TestClass.h"
TestClass test(){
    TestClass t;
    return t;
}
using namespace std;
int main(){
    TestClass t1 = test();
    cout << "letben" << endl;
    system("pause");
    return 0;
}

TestClass.h

#pragma once
class TestClass
{
public:
    TestClass();
    TestClass(const TestClass &testClass);
    ~TestClass();
};

TestClass.cpp

#include "TestClass.h"
#include <iostream>
using namespace std;
TestClass::TestClass()
{
    cout << "testClass" << endl;
}
TestClass::TestClass(const TestClass &testClass){
    cout << "copy constructor" << endl;
}
TestClass::~TestClass()
{
}

运行结果:

所以C++是语言,而java是一种产品。

产品的制式应该是有规范可言的。对于语言,不同的厂商在认定过程中,所遵循的标准也不相同,所以在不同的编译环境下,编译结果不同,在不同的操作系统下,表现形式也不同,这就引来了程序移植的概念,而java只要有java虚拟机的存在,就可以无伤跑通各种平台。

 

QT编译的时候,对代码进行了优化,那里就认为是一个对象,所以没有调用复制构造函数,但是对于vs来讲,这很明显是复制构造函数。

 

posted on 2016-03-19 14:34  木鸟飞  阅读(501)  评论(0编辑  收藏  举报

导航