gpt4 book ai didi

c++ - 与许多输入小部件接口(interface)的设计方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:58:36 25 4
gpt4 key购买 nike

我的界面有大约 16 个输入框。它们都被声明为类中的公共(public)指针,已初始化等。但是,随着我的代码越来越多地使用私有(private)函数进行数据库提交、错误检查、临时存储例程等,如果一个字段必须被删除或添加一个新的我必须深入研究所有这些私有(private)函数并明确删除/添加该字段;并且始终与字段排序有关。

更简单的方法!

这是我的想法,我希望任何人都可以否定它或在它的基础上进行构建:

我的想法是将指向所有输入字段的指针存储在一个指针数组中,然后所有这些私有(private)辅助函数遍历该数组;然而,其中一些私有(private)函数是静态的,有些是非静态的;因此需要更多的指针魔法,或者我应该拥有两个这样的指针数组函数:一个供静态函数使用,一个供非静态函数使用?

更复杂的是,在小部件上调用的方法取决于私有(private)函数正在做什么......有些人可能会调用“->value(foo)”有些人可能会调用“->location(1),-> location(2)”,按小部件的顺序递增。有没有办法将调用的方法和要传递的参数传递给这个包含输入字段指针数组的新辅助函数?

深思:也许我试图通过在需要进行更改时省去滚动所有代码的负担来变得过于花哨?也许这会增加所有额外的指针间接寻址的开销?受苦更好吗?

谢谢,任何帮助都将不胜感激。如果确实需要一些代码示例,我可以制作一些。

代码示例(这不会编译并且是徒手输入的示例)

class Foo
{
public:
InputBox * in1;
InputBox * in2;
InputBox * in3;
ExternalDataSource * exds; // Pretend this object you can retrieve values out of
private:
static void clearFieldsFunc1(void * v); // callback bound to a button
static void loadFieldFunc2(void * v); // callback bound to a button
void printFieldsFunc3(); // not a callback, just called from various functions
}

Foo::Foo()
{
in1= new InputBox (0,0,10,10); // Box x,y,w,h
in2= new InputBox (15,0,10,10);
in3= new InputBox (30,0,10,10);
exds = new ExernalDataSource("US Intelligence Agency");
}

// Clears the fields
void Foo::clearFieldsFunc1(void * v)
{
Foo * fptr = ((Foo*)v);
fptr->in1->clear();
fptr->in2->clear();
fptr->in3->clear();
}

// Loads the fields
void Foo::loadFieldFunc2(void * v)
{
Foo * fptr = ((Foo*)v);
fptr->in1->value(fptr->exds->getValue(1));
fptr->in2->value(fptr->exds->getValue(2));
fptr->in3->value(fptr->exds->getValue(3));
}

// Prints the fields
void Foo::printFieldsFunc3()
{
printf("%s\n",this->in1->value());
printf("%s\n",this->in2->value());
printf("%s\n",this->in3->value());
}

最佳答案

您可以将 InputBox 的容器添加为 Foo 的成员并对其进行迭代以使生活更简单。

#include <vector>

class Foo
{
private:
static void clearFieldsFunc1(void * v); // callback bound to a button
static void loadFieldFunc2(void * v); // callback bound to a button
void printFieldsFunc3(); // not a callback, just called from various functions

std::vector<InputBox> m_inputBoxes;
typedef std::vector<InputBox>::iterator InputItr;
};

Foo::Foo()
{
m_inputBoxes.push_back(InputBox(0, 0, 10, 10));
m_inputBoxes.push_back(InputBox(15, 0, 10, 10));
m_inputBoxes.push_back(InputBox(30, 0, 10, 10));
}

// Clears the fields
void Foo::clearFieldsFunc1(void * v)
{
for(InputItr itr(m_inputBoxes.begin()); itr != m_inputBoxes.end(); ++itr)
itr->clear(); // calls clear for each InputBox
}

// etc

关于c++ - 与许多输入小部件接口(interface)的设计方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5980702/

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