我有以下布局:
MainWindow <--------- Settings
\
\
V
PerformOps
MainWindow 接收从 Settings 传递的变量,并将它们传递给 PerformOps。如果未使用 Settings 类,则 MainWindow 将默认值传递给 PerformOps。
目前我做:
class Settings{
public:
Settings(*parent);
var1, var2, var3
.
.
void exec();
}
class PerformOps{
public:
PerformOps();
var1, var2, var3;
.
.
void start();
}
class MainWindow{
private:
Settings *set; //share pointers over all methods
PerformOps *op;
bool settings_not_clicked = false;
void saveChanges()
{
QSettings settings("my","app");
settings.setValue("value1", op->var1);
settings.setValue("value2", op->var2);
settings.setValue("value3", op->var3);
}
void loadChanges()
{
QSettings settings("my","app");
op->var1 = settings.value("value1");
op->var2 = settings.value("value2");
op->var3 = settings.value("value3");
}
void closeEvent(QCloseEvent *event)
{
event->ignore();
saveChanges();
event->accept();
}
void takeDataAndDoStuff() //This is always called
{
op = new PerformOps;
if(settings_not_clicked) {
loadChanges()
}
else {
op->var1 = 33;
op->var2 = "Geronimo!";
op->var3 = true;
}
op->start();
}
void getStuff_maybe_if_clicked() //This might not be always called
{
Settings *set = new Settings(this);
set->exec() //Pause parent, run Settings
settings_not_clicked = false;
}
op->var1 = set->var1;
问题:考虑到set
指针可能并不总是被初始化,是否有一种更简洁的方法来跨类共享数据而不使用 : 的脏方法?