一个班级可以接受仅由外部环境使用其属性
Is it acceptable for a class to have its properties used ONLY by the outside environment?
有时我们会遇到一个问题不需要使用自己的属性。请参阅方法 A:
struct Ball {
double mass = 1;
double x = 0;
double y = 0;
};
struct World {
std::vector<Ball*> balls;
void run_physics() {
// here we run the physics
// we can access every ball and their x, y properties
}
};
为了避免这种情况,我们可以使用方法 b :
struct World;
struct Ball {
World* world = NULL;
double mass = 1;
double x = 0;
double y = 0;
void run_physics() {
if (this->world != NULL) {
// here we run the physics again
// we can access every other ball properties through this->world->balls vector.
}
}
};
struct World {
std::vector<Ball*> balls;
};
但是接近B是一个紧密的耦合结构,这意味着Ball
和World
彼此都不好,这不好。
那么,哪个更好的方法?
- a :松散耦合,但是有些类不会使用自己的属性,或者
- b :类将使用其属性,但是紧密耦合结构?
什么时候使用每个?
a 更好,因为它更可扩展。
球可能具有与当前计算无关的其他属性,例如用于计算惯性矩的成员(例如,如果是空心球(。
是的,是的,可以接受仅由外部环境使用其属性,因为可能永远不会发生这种情况。
也就是说,如果x
和y
告诉您有关球的位置的信息,那么这些与一堂课有关,告诉您有关已安装的球实例的收集,而不是成为球本身的一部分。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 枚举环境变量的惯用C++14/C++17方法
- C++概念:如何使用'concept'检查模板化结构的属性?
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 通过指向指针数组的指针访问子类的属性
- 访问 PlatformIO 中的外部环境变量
- 如何在Log4CPP属性文件中使用环境变量
- 一个班级可以接受仅由外部环境使用其属性
- 具有仅在某些情况下引用外部变量的属性的类
- 将外部属性映射附加到图形
- 从外部函数访问外部结构属性
- 有没有任何方法可以从外部控制photoshop属性
- 提升图形库 - 来自外部向量的权重属性
- 如何在 ARC 环境中指定要C++指针的属性
- 如何在使用外部c++ API时创建测试环境
- Visual Studio 2010在属性表中使用环境变量
- 外部属性映射绑定到std::矢量在boost图形库
- 添加一个外部属性以包含图中顶点的索引(boost)
- boost图的外部属性表现怪异
- 将boost图中的内部属性转换为boost图的外部属性容器