如何组织与两个对象有联系的变量

How to organize variable which has connection with two objects

本文关键字:对象 联系 变量 两个 何组织      更新时间:2023-10-16

在我的代码中,有一个类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);

它充当创造者。这似乎与您当前的风格和逻辑一致。