C++奇怪的函数指针行为
C++ Weird function pointer behaviour
这是一个简单的opengl/sdl程序。通常有一个巨大的deque,其中程序保存显示数据,例如三角形将入/读入/从取消队列中读取,就像这样(GL_Begin....顶点 1 ....顶点 2 ...顶点 3 ...GL_End )
项结构
enum Shapes{
LINE,
POLYGON,
TRIANGLE
};
struct Item{
int id;
int type;
Shapes shape;
double x;
double y;
double z;
};
。
void GEngine::CC2D(int id,double x,double y){ // Change Coordinates ... Move the Item
for(int i=0;i<Items.size();i++){
Item* item = &Items[i]; // Pointer to the item INSIDE the deque because we need to edit it, we can not create a new instance of Item
if(item->id == id && item->type == 2){
item->x += x;
item->y += y;
}
}
DrawScene();
}
void GEngine::PollEvents( void (*Event)() ){
int mbpressed = 0; // Mouse button pressed flag
int mouse_xpre = 0;
int mouse_ypre = 0;
int move_id = 0; // TESTING
while(1){
while( SDL_PollEvent( &event ) ){
switch( event.type ){
case SDL_MOUSEMOTION:{
if(mbpressed == 1){
mouse_x = event.motion.x; // X2
mouse_y = event.motion.y; // Y2
// (*Event)();
// CC2D( ( X2 - X1 ),( Y2 - Y1 )
CC2D(move_id,(mouse_x-mouse_xpre),(mouse_y-mouse_ypre)); // The difference between the current and the previous mouse position is equal to the DX and DY which will be used to move the vertices.
mouse_xpre = mouse_x; // X1
mouse_ypre = mouse_y; // Y1
SDL_Delay(20);
}
break;
现在问题来了。如果我使用 CC2D(move_id,(mouse_x-mouse_xpre),(mouse_y-mouse_ypre));它工作正常,但是如果我将 CC2D 函数添加到事件中,算法会失败,而不是用鼠标移动项目,而是远离屏幕。
换句话说。
case SDL_MOUSEMOTION:{
if(mbpressed == 1){
mouse_x = event.motion.x; // X2
mouse_y = event.motion.y; // Y2
// (*Event)();
// CC2D( ( X2 - X1 ),( Y2 - Y1 )
CC2D(move_id,(mouse_x-mouse_xpre),(mouse_y-mouse_ypre)); // The difference between the current and the previous mouse position is equal to the DX and DY which will be used to move the vertices.
mouse_xpre = mouse_x; // X1
mouse_ypre = mouse_y; // Y1
SDL_Delay(20);
}
break;
}
^ 工作正常。
但
GEngine gEngine;
void Event(){
gEngine.CC2D(0,(gEngine.mouse_x-gEngine.mouse_xpre),(gEngine.mouse_y-gEngine.mouse_ypre));
}
int main(int argc, char *argv[]){
... code ...
gEngine.PollEvents(&Event);
return 0; // NEVER REACHED
}
跟
case SDL_MOUSEMOTION:{
if(mbpressed == 1){
mouse_x = event.motion.x; // X2
mouse_y = event.motion.y; // Y2
(*Event)();
mouse_xpre = mouse_x; // X1
mouse_ypre = mouse_y; // Y1
SDL_Delay(20);
}
break;
}
不。。。
为了进一步简化事情:
Case SDL_MOUSEMOTION: ...... code ..... CC2D( params) ........
工作正常,但
Event(){ CC2D( params ) } // Main.cpp
Case SDL_MOUSEMOTION: ...... code ..... (*Event)() ........ // GEngine.cpp
无法按预期工作
您在PollEvents
中声明局部变量:
int mouse_xpre = 0;
int mouse_ypre = 0;
请注意,它们被命名为 mouse_xpre
和 mouse_ypre
。然后在你正在做的回调中
gEngine.CC2D(0,(gEngine.mouse_x-gEngine.mouse_xpre),(gEngine.mouse_y-gEngine.mouse_ypre));
// ^^^^^^^^^^ ^^^^^^^^^^
访问成员变量。您需要删除局部变量的声明,以便它们不会隐藏成员变量,并且您将在两个位置使用成员变量。
相关文章:
- 如何正确编写指针函数声明?
- C++常规指针函数或模板
- 如何重新定义 C++ 指针函数?
- C++ 指向其他类函数的指针函数
- 指针到指针函数参数
- 将指向成员的指针函数传递到模板中
- C++ 在 none 常量指针函数中返回一个常量指针
- 如何使用指针函数编写/读取数组
- 是C 中的函数指针函数对象
- 如何从另一个类调用指向成员的指针函数
- 如何声明采用指向成员的指针函数的函数
- 如何构造一个以可变参数指针函数作为成员的类?
- c 通过值或指针函数语法
- 将typedef方法作为指针函数传递
- 从 Main 中的双指针函数打印出指针数组
- strcpy 对指针函数的引用
- 这是否仍然声明一种指针函数的别名?
- 将指向成员的指针函数与 std::shared_ptr 结合使用
- 'Incomplete type' 为标准::函数声明指向成员的指针函数模板参数时出错
- 从注入进程的 DLL 调用函数并更改指针函数的地址