DialogProc函数要求声明它自身静态[重复](DialogProc function asking to declare it self static [duplicate])
这个问题在这里已有答案:
- 静态函数 4中 无法访问的非静态成员 答案
这是我的createialogparam函数,它从这里调用DialogProc函数 -
HRESULT AMEPreviewHandler::CreatePreviewWindow() { assert(m_hwndPreview == NULL); assert(m_hwndParent != NULL); HRESULT hr = S_OK; m_hwndPreview = CreateDialogParam( g_hInst,MAKEINTRESOURCE(IDD_MAINDIALOG), m_hwndParent,(DLGPROC)DialogProc, (LPARAM)this); /here the dialog proc function is called if (m_hwndPreview == NULL) { hr = HRESULT_FROM_WIN32(GetLastError()); } .......... ... }
这是DialogProc函数的定义 -
BOOL CALLBACK AMEPreviewHandler::DialogProc(HWND m_hwndPreview, UINT Umsg, WPARAM wParam, LPARAM lParam) { static RECT m_rcParent ; switch(Umsg) { case WM_INITDIALOG: { return 0; } break; ........ case WM_COMMAND: { int ctl = LOWORD(wParam); int event = HIWORD(wParam); if (ctl == IDC_PREVIOUS && event == BN_CLICKED ) { CreateHtmlPreview(); //it must be static now and it is not able to access the non static vraibles delared globally in the program return 0; } } }
而声明是这样的 -
static BOOL CALLBACK DialogProc(HWND hWindow,UINT uMsg,WPARAM wParam,LPARAM lParam); //假设它是静态的..如果静态则没有给出任何错误。如果它没有被声明为静态,则会给出错误
这里 -m_hwndPreview = CreateDialogParam( g_hInst,MAKEINTRESOURCE(IDD_MAINDIALOG), m_hwndParent,(DLGPROC)DialogProc, (LPARAM)this); //error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'DLGPROC'
有没有办法在静态DialogProc中访问全局声明的变量,或者可以访问dialogproc中的全局声明的变量而不将这些变量声明为静态,因为它们在程序的其他部分也被用作非静态变量?
This question already has an answer here:
here is my createdialogparam function which is calling DialogProc function from here-
HRESULT AMEPreviewHandler::CreatePreviewWindow() { assert(m_hwndPreview == NULL); assert(m_hwndParent != NULL); HRESULT hr = S_OK; m_hwndPreview = CreateDialogParam( g_hInst,MAKEINTRESOURCE(IDD_MAINDIALOG), m_hwndParent,(DLGPROC)DialogProc, (LPARAM)this); /here the dialog proc function is called if (m_hwndPreview == NULL) { hr = HRESULT_FROM_WIN32(GetLastError()); } .......... ... }
here is the definition of DialogProc function-
BOOL CALLBACK AMEPreviewHandler::DialogProc(HWND m_hwndPreview, UINT Umsg, WPARAM wParam, LPARAM lParam) { static RECT m_rcParent ; switch(Umsg) { case WM_INITDIALOG: { return 0; } break; ........ case WM_COMMAND: { int ctl = LOWORD(wParam); int event = HIWORD(wParam); if (ctl == IDC_PREVIOUS && event == BN_CLICKED ) { CreateHtmlPreview(); //it must be static now and it is not able to access the non static vraibles delared globally in the program return 0; } } }
and the declaration is like this-
static BOOL CALLBACK DialogProc(HWND hWindow, UINT uMsg, WPARAM wParam, LPARAM lParam); //suppose it is static ..it is not giving any error if static ..If it is not declared static it gives error
here -m_hwndPreview = CreateDialogParam( g_hInst,MAKEINTRESOURCE(IDD_MAINDIALOG), m_hwndParent,(DLGPROC)DialogProc, (LPARAM)this); //error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'DLGPROC'
Is there any way to access the globally declared variables inside the static DialogProc or it is possible to access the globally declared variables inside the dialogproc without declaring the those variables as static because t hey are also used as non static in other part of program ??
原文:https://stackoverflow.com/questions/17790479
最满意答案
- 冗余呼叫
ensureIndex
是空操作。- 如果
ensureIndex
调用中引用的集合不存在,它将被创建。- 一次就够了。
- 除非您指定
background: true
选项,否则创建索引会阻止其他数据库操作。MongoDB 3.0更新
ensureIndex
在v3.0中已弃用,现在是createIndex
的别名,因此应该使用createIndex
。 行为保持不变。
- Redundant calls to
ensureIndex
are no-ops.- If the collection referenced in the
ensureIndex
call doesn't exist it will be created.- Once is enough.
- Unless you specify the
background: true
option, creating an index blocks other database operations.MongoDB 3.0 Update
ensureIndex
was deprecated in v3.0 and is now an alias forcreateIndex
, socreateIndex
should be used instead. The behavior remains the same.
相关问答
更多-
更新2:原始答案以及第一次更新错误地引用了Mongo shell文档,而不是Java API。 在Java中, DBCollection.ensureIndex()在版本2.12中被弃用,并在版本3.0中被删除。 DBCollection.createIndex()是应该使用的。 更新: 从3.0.0版开始,不推荐使用db.collection.ensureIndex() 。 现在是db.collection.createIndex()的别名。 原版的: 自1.8开始, createIndex()已弃用 它 ...
-
看来我的评论有点被误解了,所以我会澄清一下。 只要你在第一次调用find()之前调用它, 那么调用它并不重要。 换句话说,创建索引时无关紧要,只要它在您期望使用它之前就已经存在了。 我见过很多的常见模式是在find()调用的同时(和在同一个地方)对ensureIndex进行编码。 ensureIndex将检查索引是否存在,如果不存在则创建它。 在调用find()之前调用ensureindex毫无疑问存在一些开销(虽然非常小),所以最好不要这样做。 我在代码中调用ensureIndex来简化部署并避免单独管理 ...
-
请记住,在Mongo 3.x中, sureIndex已被弃用,应该不鼓励。 自3.0.0版本以来已弃用:db.collection.ensureIndex()现在是db.collection.createIndex()的别名。 同样在pymongo : DEPRECATED - 确保此集合上存在索引。 这意味着您应该始终使用create_index 。 Keep in mind that in Mongo 3.x ensureIndex is deprecated and should be discour ...
-
在mongodb中确保索引(ensureIndex in mongodb)[2022-12-27]
冗余呼叫ensureIndex是空操作。 如果ensureIndex调用中引用的集合不存在,它将被创建。 一次就够了。 除非您指定background: true选项,否则创建索引会阻止其他数据库操作。 MongoDB 3.0更新 ensureIndex在v3.0中已弃用,现在是createIndex的别名,因此应该使用createIndex 。 行为保持不变。 Redundant calls to ensureIndex are no-ops. If the collection referenced i ... -
MongoDB索引(MongoDB indexes)[2022-06-15]
是的,MongoDB中的_id始终是唯一的。 它是主键,这就是为什么不需要设置UNIQUE原因。 Yes, _id in MongoDB is always unique. It's the primary key, which is why setting UNIQUE isn't necessary. -
MongoDB复杂索引(MongoDB complex indices)[2022-01-02]
我应该创建这样的复合索引: db.items.ensureIndex({age:1,brand:1,零售商:1,性别:1,样式:1,位置:1,颜色:1,关键字:1}) 您可以创建一个类似上面的索引,但您几乎将整个集合编入索引。 索引占用空间; 索引中的字段越多,使用的空间就越多。 通常是RAM,虽然它们可以换掉。 他们还会受到处罚。 您的索引似乎很浪费,因为可能只索引其中一些字段会使MongoDB扫描一组接近查找操作预期结果的文档。 我可以创建一组更好的索引来优化此查询吗? 就像我之前说过的那样,可能是的。 ... -
在当前的Mongoose 4.3.7版本中,您不能在方案中定义部分索引,但仍然可以使用MongoDB 3.2的部分索引。 您只需使用本机驱动程序创建索引。 // ScheduleModel is a Mongoose Model ScheduleModel.collection.createIndex({"type" : 1 } , {background:true , partialFilterExpression : { type :"g" }} , function(err , result){ ...
-
Mongodb复合索引可确保嵌入文档中的唯一性(Mongodb compound index to ensure uniqueness within embedded documents)[2022-11-03]
目前,索引(无论是否唯一)对整个集合起作用,而不是每个子文档。 如果你想在子文档上有某种独特性,你可以使用查询魔术: db.c.update({ name: 'Sammaye', 'permission.group': {$ne: ObjectId()} }, {$push:{permission:{group: ObjectId(), u: 31}}}); 这当然只会在sammaye尚未具有该特定group条目时更新。 随意根据需要进行修改。 Currently indexes, uni ... -
MongoDB每个查询一次只能使用一个索引,因此您创建两个索引的建议将无效。 相反,我会创建一个复合索引: > db.foo.ensureIndex({"tag_cloud.value": 1, "tag_cloud.count": -1}) 此索引既可以用于过滤到您要考虑的特定元素,也可以按降序排序返回: > db.foo.find({"tag_cloud.value":"games"}).sort({"tag_cloud.count":-1}).explain() { "cursor" : " ...
-
使用MongoDB的应用程序重新启动。 随后对ensureIndex()的调用是否会导致完整的重新索引? 不,它应该(如在其他所有驱动程序中)注册为无操作,因为索引已经存在。 一些驱动程序提供了一种缓存机制,可以在不进入服务器的情况下检测是否已创建索引(即Python)。 MongoDB服务器重新启动。 稍后从客户端应用程序调用ensureIndex()会重建吗? 与上面相同 是否有多个客户端会话影响了这些? 我假设根据文档,索引在整个集合中是全局的:“MongoDB在每个集合级别定义索引。” 是的索引存储 ...