如何组织与两个对象有联系的变量
How to organize variable which has connection with two objects
在我的代码中,有一个类Func,它表示一个函数。类 FuncSet 是类 Func 中一些函数的集合。有两个对象 FuncSet set1 和 FuncSet set2。
现在我想组成一个变量,它是一个 2D 矩阵,并将所有函数的卷积存储在 set1 和 set2 中。我不知道哪种是组织此变量的最佳方法。如果我将其声明为类 FuncSet 中的成员,这在逻辑上没有意义,因为它在两个对象之间有一些联系。
// class of function
class Func
{
// calculate convolution of this function and function v
double convolution(const Func & v);
}
// class of set of functions
class FuncSet
{
std::vector<Func> func;
}
// two objects set1 and set2, each of them has some Func objects.
// For example, set1 has 10 functions and set2 has 20 functions
FuncSet set1(10);
FuncSet set2(20);
// calculate convolution of u and v, for all functions u in set1 and all functions v in set2
// it should be a 10*20 double 2D matrix
// where should I store this 2D matrix?
我会把它存储在你使用它的地方。
如果你有一些通过简单(级联)函数组织的算法,那么你就在一个函数中声明它。
如果你有其他一些类,它有一些任务,一些状态等,你可以在那里创建一个包含这些函数的成员变量。
至于如何组织"卷积"(笛卡尔乘积对我来说更清楚),你可以在本地有一些vector<vector<Func>>
,或者你可以把它放在一些额外的类FuncSetConvolution
或类似的东西中。
什么是最佳解决方案取决于您如何继续使用它。通常,我会努力将vector<vector<Func>>
(用于具有函数)或vector<vector<double>>
每个组合的值与计算结果放入某个额外的类中。然后你可以编写 free 函数:
FuncSetConvolution convolute(const FuncSet& set1, const FuncSet& set2);
它充当创造者。这似乎与您当前的风格和逻辑一致。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 将共享对象库与Eclipse(氧)联系起来
- 如何组织与两个对象有联系的变量