我想要一些关于班级设计的建议。假设我有 3 个班级,"class A", "class B" and "class C"
. 每个类都有不同的命名空间。
"A" has an instance of "B", and "B" has an instance of "C"
. 每个类都有一个“结构设置”,每个类都使用 SetSettings() 进行设置。其实,"A" uses "B" to do its job, and "B" uses "C" to do its job
。
我的问题是,有没有更好的方法来进行这些层次结构设置?
例如,to break the relation between "A" and "C", "B"
可以有相同的参数"C::Settings"
而不是定义一个 c_settings...
提前致谢!
啊
#include "B.h"
namespace A {
struct Settings {
int param_for_A_1;
B::Settings b_settings;
};
class A {
void SetSettings(const Settings& source) {
settings_ = source;
b_.SetSettings(source.b_settings);
}
Settings settings_;
B::B b_;
};
}
溴化氢
#include "C.h"
namespace B {
struct Settings {
int param_for_B_1;
int param_for_A_2;
C::Settings c_settings;
};
class B {
void SetSettings(const Settings& source) {
settings_ = source;
c_.SetSettings(source.c_settings);
}
Settings settings_;
C::C c_;
};
}
通道
namespace C {
struct Settings {
int param_for_C_1;
};
class C {
void SetSettings(const Settings& source) {
settings_ = source;
}
Settings settings_;
};
}
主文件
#include "A.h"
int main() {
A::Settings settings;
// Hierarchy settings...
settings.param_for_A_1 = 1;
settings.b_settings.param_for_B_1= 2;
settings.b_settings.param_for_B_2 = 3;
settings.b_settings.c_settings.param_for_C_1= 4;
class A::A a;
a_.SetSettings(settings);
return;
}