我有一个类参数,其目的是为了表示某个参数可以容纳的可能值(实现两个关键方法,GetNumValues()和GetValue(int index))。撰写您继承的类的对象?
通常一个逻辑参数(参数值是位标志)最好由2个或更多的Parameter类实例表示(即参数可以是1或2,参数可以是4或8,而不是一个参数可以是5,6,9或10)。为了处理这个问题,我想创建一个包含Parameters的CompositeParameter类,并且将基于它所包含的参数的组合实现GetNumValues()和GetValue()函数。
而且由于CompositeParameter组合参数使它们充当单个参数,所以“CompositeParameter是参数”关系是合理的。所以我发现自己处于这样一种情况,即我有一个类组成它继承的类的对象,这看起来不正确。但同时,我不明白为什么更高级别的代码不应该能够将CompositeParameters和Parameters完全相同。
我能想到的唯一选择是让CompositeParameter简单地组成参数,而更高级别的代码只会处理CompositeParameters。然而,这是有点浪费的B/C一般情况下将是仅包含一个参数的CompositeParameters。
想法?
class Parameter
{
public:
virtual unsigned int GetNumValues() const {...}
virtual unsigned int GetValue(unsigned int index) const {...}
}
class CompositeParameter : public Parameter
{
public:
// product of GetNumValues() of each item in mParamList
virtual unsigned int GetNumValues() const {...}
// allow all the possible combinations of the items in mParamList to be
// treated as one parameter. i.e. if mNumParams = 2, this would be analogous
// to getting the row and col index of a matrix from index, and combining
// the mParamList[0]->GetValue(row) and mParamList[1]->GetValue(col)
virtual unsigned int GetValue(unsigned int index) const {...}
private:
static const unsigned int MAX_PARAMS = 10;
unsigned int mNumParams;
const Parameter* mParamList[MAX_PARAMS];
}
在谈论C++代码时,使用C++代码而不是英语来描述问题几乎总是比较好的。 – 2010-02-03 23:12:45