gpt4 book ai didi

c++ - 编写此类程序的最佳方法 "writing the code "::Qt

转载 作者:行者123 更新时间:2023-11-30 02:47:37 24 4
gpt4 key购买 nike

你们能帮我解答这些问题并给出你们的意见吗

1- 假设我想创建一个可以计算某些东西的窗口“任何窗口”如果我在构造函数中编写大部分内容而不是创建一个为我完成它然后在收缩器中调用它的函数是否可以?或者哪种方法更好。

2- 假设我有一个数组或 qvector 或任何东西,可能有 +30 个元素,这些元素应该是常量可能就像持有主题名称“我想插入名称而不是用户”,这被认为是不好的像这样初始化它:

s[0]=".."
s[1]="...."

因为我不能使用循环,因为名称会改变。

最佳答案

1- let's say I want to create a window that maybe calculate something " any window " is it ok if I write most of the stuff in the constrictor instead of creating a function that does it for me then call it in the constrictor ? or which is the better approach.

我会告诉你,任何 函数都应该很小,而不仅仅是构造函数。 This可能有帮助。

2- let's say I have an array or qvector or anything , with maybe +30 elements , those elements are meant to be constant maybe like holding subjects names " I want to insert the names not the user " , is it considered bad to inizilize it like this :

s[0]=".." s[1]="...." . . .

since I can't use a loop because the names will change.

考虑在外部配置文件中指定这些值,然后您将在应用程序启动时加载(即解析)这些值。也就是说,而不是像

这样的语句
s[0] = "john doe"
s[1] = "jane doe
// ...

将这些名称放在一个文件中,例如 names.cfg,然后像这样填写您的 QVector:

std::ifstream in{"names.cfg"};

QVector<std::string> names;
std::string current_name;

while (std::getline(in, current_name))
names.push_back(current_name);

免责声明:我不是 Qt 专家,但我认为 QVector 的使用是有效的。

关于c++ - 编写此类程序的最佳方法 "writing the code "::Qt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22619868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com