读书人

为什么这样写出来的类构造函数中的没有

发布时间: 2012-03-02 14:40:29 作者: rapoo

为什么这样写出来的类构造函数中的没有输出?
#include <iostream.h>
class say
{
public:
say()
{
cout < < "hell ";
}
~say()
{
cout < < "world! ";
}
}hello;
int main()
{
cout < < "o ";
return 0;
}
这样编译出来的程序结果是:hello world!

而下面的编译出来的程序结果是:hello
#include <iostream>
using std::cout;
class say
{
public:
say()
{
cout < < "hell ";
}
~say()
{
cout < < "world! ";
}
}hello;
int main()
{
cout < < "o ";
return 0;
}

[解决办法]
#include <iostream>
using std::cout;
class say
{
public:

say()
{
cout < < "hello ";
}
~say()
{
cout < < "world! ";
}
};

int main()
{
say hello;
cout < < "o " < < endl;
return 0;
}

这样试试应该就是 "hello world "了,自己分析一哈为什么呀?
[解决办法]
给你一个解决方案
你放到vc2005下运行一下就是了

[解决办法]
没看懂二楼的是为什么。。。
====================
二楼的代码还lz的代码只不过是对象产生的地方及其作用域不同而已
[解决办法]
编译器问题!
[解决办法]
编译器问题,cout如果先比你的全局变量析构,那么你是看不到结果的.
如果用printf这个输出就肯定没有问题.

读书人网 >C++

热点推荐