好或坏:创建巨大的结构以避免使用全局变量/结构的属性/大量参数

发布于 2025-01-02 17:35:38 字数 643 浏览 0 评论 0原文

我是一个初学者,我已经用 C++ 编写了一个 10000 行的程序,该程序主要使用全局变量和 void 函数。

无论如何,我的程序没有 GUI,所以我正在使用 Clutter 为其制作一个 GUI。因此,在混乱中,您可以使用信号处理函数来连接按钮点击、运动事件等。

信号处理函数只能接受一个用户数据参数。然而,GUI 的组件很多,数百个需要通过不同的功能来访问。因此,我将所有 GUI 对象放在一个结构中,并从每个信号处理函数传递它。

所以我的程序现在是这样的,(控制台程序)打印按下一些字母来做某事。如果您按下该字母,则会启动某个功能。如果我消除全局的使用,我将需要将其中一些变量作为参数传递。

如果我直接将代码插入 GUI,那么信号处理函数将启动适当的函数,但只能传递单个用户数据参数,而现在,该参数已被用作具有数百个 GUI 成员的结构。

抱歉,如果这一切听起来很疯狂。我只是想重新编写我的代码以使用更好的实践,但是对于 10000 长的代码,以及我对某些事情缺乏理解,我感到相当不知所措。

我只是在寻找一些关于从哪里开始以及如何处理我在连接到 GUI 时发现的问题的建议。

对于我关于结构的问题。我有兴趣知道结构体中是否可以包含最大数量的元素。如果结构中有一个数组,该数组的访问时间是否会变慢?结构体的内存处理方式是否存在差异?

谢谢。

I'm kind of a beginner, and I have gone off written a 10000 line program which mostly uses globals and void functions, in c++.

Anyways, my program doesn't have a GUI, so I am making one for it using Clutter. So in clutter, you use a signal handling function to connect button clicks, motion events, etc.

The signal handling function can only accept one user data parameter. However, many components of the GUI, hundreds need to be accessed by different functions. So I'm putting all my GUI objects in a single struct, and passing it from every signal handling function.

So my program as it is now, (console program) prints press some letter to do something. If you press that letter, launch a certain function. If I eliminate the use of global, I would need to pass as parameters some of these variables.

If I directly insert my code into the GUI, then the signal handler function will launch the appropriate functions, but can only pass a single user data parameter, which as it is now, is already used as a struct with hundreds of GUI members.

Sorry If this all sounds crazy. I'm just trying to re-write my code to use better practices, but with the 10000 long code, and my lack of understanding of some things, I feel pretty overwhelmed.

I'm just looking for some advice about where to start and how to deal with this issue I am perceiving with connecting to the GUI.

And for my question about structs. I am interested in knowing if there is a maximum number of elements that can be inside a struct. If you have an array inside struct, is the access time for that array going to be slower? Is there a difference in how memory is handled for a struct.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2025-01-09 17:35:39

我认为你的问题有一个隐藏的问题:如何编写GUI应用程序/框架?

从我快速谷歌搜索的内容来看,混乱程度似乎很低。如果您想跳过大量编码,只需使用 Qt Framework 之类的东西,它也使用 OpenGL(或 GTK+,取决于您想要哪种许可证)渲染大多数内容。

如果你想或多或少地从头开始写它,那么没有简单的答案,我会尝试在一个非常高的水平上回顾它,并强调它通常如何工作的非常非常基本的想法。

既然你用 C++ 标签来标记它,那么正确的 C++ 方法就是使用类。

您将拥有声音 (MP3) 之类的类,其中包含解码、编码等方法。然后,您将拥有 GUI 元素的类,包含 onPaint、onClick 等方法。

假设您想要一个按钮。它将是一个包含 x、y、宽度、高度、文本、clickHandler、enabled 等变量的类。它会有一些低级事件处理程序,例如 onPaint、onClick(这将检查鼠标是否位于按钮上以及是否已启用,如果所有这些检查都通过,它将调用 clickHandler)。它还可能有一些方法,如 isEnabled()、disable()、enable() 等。

现在您可以随意创建任意数量的按钮实例,每个实例都有自己的状态(启用或禁用、文本、宽度) ,身高,所有这些)。您将为每个按钮设置一个 clickHandler。

将会有一些全局变量,但大多数内部内容都在类内部。如果您有某种层次结构,例如窗口有许多按钮,那么您的 Window 类将只有一个按钮列表。并且不要忘记每个类都应该位于一个新文件中,除非有多个非常密切相关的类。

但实际上您不想编写任何内容,因为已经有巨大的库在这样做。

至于如何将它们粘合在一起,您可能想要遵循您选择的特定框架使用的约定。如果您使用的是 Windows 并且想要制作仅限 Windows 的应用程序,那么请使用 WPF(或 Winforms)。 Qt 非常适合跨平台,而且非常现代,但如果您正在制作商业应用程序,则许可证会有点限制。

I think your question has a hidden question: How to write GUI application/framework?

Clutter seems very low level from what I quickly Googled. If you want skip a lot coding just use something like Qt Framework which also renders most stuff using OpenGL (or GTK+, depends what kind of license you want).

There's no simple answer if you want to write it more or less from ground up and I'll try to just go over it at a very high level and just highlight the very very basic idea how it usually works.

Since you tagged it with C++ tag, then proper C++ way is to use classes.

You would have classes for things like Sound (MP3), with methods such a decode, encode, etc. Then you would have classes for GUI elements, with methods such as onPaint, onClick, etc.

Let's say you wanted a button. It would be a class with variables such as x, y, width, height, text, clickHandler, enabled and so on. It would have some low level event handlers such as onPaint, onClick (this one would check if the mouse is over the button and if it's enabled, and if it all those checks pass would it would call the clickHandler). It might also have some methods like isEnabled(), disable(), enable(), etc.

Now you're free to make as many button instances as you want, each one has it's own state (enabled or disabled, text, width, height, all of those). You would set a clickHandler for each button.

There are going to be some globals, but most of internal stuff is inside the classes. If you have some sort of hierarchy such a window has many buttons then your Window class would just have a list of buttons. And don't forget classes should each be in a new file unless there are multiple very closely related.

But really you don't want to write anything of this as there are huge libraries already doing it.

As for how to glue it all together you'd probably want to follow the convention used by the specific framework you choose. If you're on Windows and want to make Windows only application then use WPF (or Winforms). Qt is nice for cross-platform stuff and it's quite modern, but the license is a bit constraining if you're making a commercial app.

软糖 2025-01-09 17:35:39

访问结构体内部的元素总是比较慢,因为需要执行额外的引用,因此需要对 RAM 进行额外的读取。尝试将对象分组到结构体中,以便将它们一起使用,以便可以将这些内存请求缓存在更快的 RAM/板载缓存中。

结构成员实际上并没有限制,当您超过函数应采用的最大参数数量时,它们通常很有用,我(在 2002 年)被告知神奇的数字是 7。

最终,当将成员分组为 时structs,您应该能够识别哪些函数使用它们,并且您可以使该函数成为成员,而不是传递该结构,并且不必取消引用该结构来获取该参数的成员。我认为此时您可能可以为 struct 授予等效的 C++ 标签 class

Accessing an element inside a struct is always slower by the cost of having an additional reference to perform, and thus an extra read to RAM. Try grouping objects into structs so that they will be used together so that these memory requests can be cached in faster RAM/onboard cache.

There isn't really a limit to struct members and they are often useful when you exceed the maximum number of parameters a function should take, I was taught (in 2002) the magic number is 7.

Ultimately, when grouping members into structs you should be able to identify which functions use them and instead of passing the struct you can make the function a member and do away with it having to dereference the struct to get at that parameter's members. I think at this point you can probably award the structs the equivalent C++ label, class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文