在c / c ++中打印main()之前和之后的内容

时间:2014-09-01 11:06:54

标签: c++

考虑代码段

main()
{
  cout << "Hello";
} 

不触及上面的代码段打印

您好
你好
再见

2 个答案:

答案 0 :(得分:4)

#include <iostream>
using namespace std;
struct A
{
    A() { cout << "Hi" << endl; }
    ~A() { cout << endl << "Bye"; }
};

A a;

main()
{
  cout << "Hello";
} 

Live demo link.

答案 1 :(得分:1)

#include <iostream>
using namespace std;

struct A
{
    A() { cout << "Hi" << endl; }
    ~A() { cout << "\nBye" << endl; }
};

A a;


int

// untouched part :)
main()
{
    cout << "Hello";
}

输出

Hi
Hello
Bye