简体   繁体   English

如何在S ++中将指向SDL_Surface的“全局”指针传递给类的渲染函数?

[英]How can I pass a “global” pointer to a SDL_Surface to a render-function of a class in C++?

My following problem is related to my previous question here . 我接下来的问题与这里的上一个问题有关。

I want to pass a "global" screen to a render-function of a class. 我想将“全局”屏幕传递给类的渲染功能。 screen has to be a pointer to a SDL_Surface, because the initial function SDL_SetVideoMode returns a pointer to a SDL_Surface. 屏幕必须是指向SDL_Surface的指针,因为初始函数SDL_SetVideoMode返回指向SDL_Surface的指针。

Should I pass screen (set up in int main(){}) by reference to the render-function? 是否应该通过引用渲染功能传递屏幕(在int main(){}中设置)?

void renderClass::render (SDL_Surface &screen){...}

Edit: Or should I use a pointer?: 编辑:还是我应该使用一个指针?:

void renderClass::render (SDL_Surface *screen){...}

Or should I simply use 还是我应该简单地使用

 extern SDL_Surface *screen;

in each header-file. 在每个头文件中。 (Since I'm using multiple source files) (由于我使用了多个源文件)

Or is there any better way? 还是有更好的方法? I just want to get rid of pointers. 我只想摆脱指针。

I would pass it along. 我会通过。 If you ever need to have two screens (or windows) rendered using the same function, you'd have to re-architect [aside from the horribleness of using global variables in general]. 如果您需要使用同一功能渲染两个屏幕(或窗口),则必须重新架构(除了通常使用全局变量的可怕性之外)。

Edit: And I don't see any point in making it a pointer instead of a reference. 编辑:而且我看不出任何要点使其成为指针而不是引用。 Underneath things, it will be the same thing. 在事物的底下,将是一回事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM