我聲明一個函數結構內的對象,我想訪問該函數之外的所有對象(使它們成爲全局的)。這是我想要做的一個例子聲明一個函數內部的全局對象(C++)
#include <iostream>
using namespace std;
struct exampleStruct
{
int ID;
};
void makeExampleStruct()
{
exampleStruct exampleObj[30];
for (int r=0;r<=30;r++)
{
exampleObj[r].ID=r;
}
}
int main()
{
makeExampleStruct();
cout << exampleObj[1].ID << endl;
return 0;
}
這給了我一個''exampleObj'沒有在這個範圍內聲明。我知道我可以簡單地在main中聲明對象的創建,但是我寧願將它放在函數內部,這樣就更容易閱讀。有沒有辦法做到這一點?
謝謝您的幫助