程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++ 程序不一定從 main 處開始執行

C++ 程序不一定從 main 處開始執行

編輯:關於C++

C++程序我們一般寫程序都知道,是從main開始執行,不過,也有例外,比如以下這段程序

#include <iostream>  
#include <stdlib.h>  
using namespace std;  
      
class A {  
public:  
    A() {  
    cout << "I come here before main()!" << endl;  
    f();  
    }  
    static void f() {  
        cout << "I come here before main() too!" << endl;  
    }  
};  
static A a;  
          
int main(int argc, char *argv[])  
{  
  cout << "Entering main()!" << endl;  
  cout << "Leaving main()!" << endl;  
  system("PAUSE");  
  return 0;  
}

運行結果如下:

由於a是全局變量,所以會在main之前執行,所以會調用其構造函數,輸出main之前的兩句話。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved