天道酬勤,学无止境

global-scope

How to declare at beginning of program

In the listing below, an attempt to declare the rectangle "r" before the main() function is called results in an error. error: 'r' does not name a type r.x = 150;<br> Why must "r" be declared after main()? #include <SDL2/SDL.h> int main (int argc, char** argv) { // Creat a rect at pos ( 50, 50 ) that's 50 pixels wide and 50 pixels high. SDL_Rect r; r.x = 150; r.y = 150; r.w = 200; r.h = 100; SDL_Window* window = NULL; window = SDL_CreateWindow ("SDL2 rectangle", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN ); // Setup renderer SDL_Renderer* renderer = NULL

2021-05-14 18:24:17    分类:问答    c   initialization   global   global-scope

ViewState与Session…在页面生命周期中维护对象(ViewState Vs Session … maintaining object through page lifecycle)

问题 有人可以解释一下ViewState和Session之间的区别吗? 更具体地说,我想知道在页面的整个生命周期中保持对象可用(通过回发不断设置成员)的最佳方法。 我目前正在使用Sessions来执行此操作,但是我不确定这是否是最好的方法。 例如: SearchObject searchObject; protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { searchObject = new SearchObject(); Session["searchObject"] = searchObject; } else { searchObject = (SearchObject)Session["searchObject"]; } } 这使我可以在页面上的其他任何地方使用searchObject,但这有点麻烦,因为如果我更改任何属性等,我必须重置会话变量。 我认为必须有一种更好的方法来执行此操作,以使.NET不会在每次页面加载时重新实例化该对象,而是将其置于Page类的全局范围内? 回答1 如果搜索对象的大小不是很大,则可以使用ViewState。 如果只希望对象在当前页面的生命周期中存在,则ViewState是完美的。 会话对象也可以使用,但是很显然,一旦搜索对象位于其中

2021-05-07 21:10:20    分类:技术分享    c#   .net   session   viewstate   global-scope

文件范围和全局范围之间的区别(Difference between File Scope and Global Scope)

问题 我是一名学生,对C和C ++中的全局变量和文件范围变量感到困惑。 两种观点有什么不同吗? 如果是,请详细说明。 回答1 具有文件范围的变量可以由单个文件中的任何函数或块访问。 要声明文件作用域变量,只需在块外部声明变量(与全局变量相同),但使用static关键字即可。 static int nValue; // file scoped variable float fValue; // global variable int main() { double dValue; // local variable } 文件范围的变量的行为与全局变量完全相同,不同之处在于它们的使用仅限于声明它们的文件。 回答2 当实际上有多个翻译单元时,说明文件(或更准确地说,翻译单元)作用域与全局作用域之间的关系也许更清晰... 取2个文件(每个文件都是自己的翻译单元,因为它们互不包含) other.cpp float global_var = 1.0f; static float static_var = 2.0f; main.cpp #include <cstdio> extern float global_var; //extern float static_var; // compilation error - undefined reference to 'static_var' int

2021-04-30 11:11:03    分类:技术分享    c++   c   scope   global-scope

ViewState Vs Session … maintaining object through page lifecycle

Can someone please explain the difference between ViewState and Session? More specifically, I'd like to know the best way to keep an object available (continuously setting members through postbacks) throughout the lifecycle of my page. I currently use Sessions to do this, but I'm not sure if it's the best way. For example: SearchObject searchObject; protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { searchObject = new SearchObject(); Session["searchObject"] = searchObject; } else { searchObject = (SearchObject)Session["searchObject"]; } } that allows me to use my

2021-04-08 17:05:03    分类:问答    c#   .net   session   viewstate   global-scope

Difference between File Scope and Global Scope

I am a student and I am confused about global and file scope variables in C and C++. Is there any difference in both perspectives? If yes, please explain in detail.

2021-04-06 16:21:34    分类:问答    c++   c   scope   global-scope