Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 组成继承自的类的对象?_C++_Inheritance_Composition - Fatal编程技术网

C++ 组成继承自的类的对象?

C++ 组成继承自的类的对象?,c++,inheritance,composition,C++,Inheritance,Composition,我有一个类参数,其目的是表示某个参数可能持有的值(实现两个关键方法,GetNumValues()和GetValue(int-index)) 通常,一个逻辑参数(参数值为位标志)最好由参数类的两个或多个实例表示(即,一个参数可以是1或2,一个参数可以是4或8,而不是一个参数可以是5、6、9或10)。为了处理这个问题,我想创建一个包含参数的CompositeParameter类,并基于它所持有的参数组合实现GetNumValues()和GetValue()函数 而且,由于CompositeParam

我有一个类参数,其目的是表示某个参数可能持有的值(实现两个关键方法,GetNumValues()和GetValue(int-index))

通常,一个逻辑参数(参数值为位标志)最好由参数类的两个或多个实例表示(即,一个参数可以是1或2,一个参数可以是4或8,而不是一个参数可以是5、6、9或10)。为了处理这个问题,我想创建一个包含参数的CompositeParameter类,并基于它所持有的参数组合实现GetNumValues()和GetValue()函数

而且,由于CompositeParameter将参数组合在一起,使它们作为单个参数,因此“CompositeParameter是一个参数”关系是有意义的。所以我发现自己处于这样一种情况下,我有一个类,它由它继承的类的对象组成,这似乎是不对的。但同时,我不明白为什么更高级别的代码不能完全相同地处理CompositeParameters和Parameters

我能想到的唯一选择是让CompositeParameter简单地组合参数,而更高级别的代码只处理CompositeParameters。然而,这有点浪费b/c。一般情况下,复合参数只包含一个参数

想法

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];
}

这似乎是一个非常合理的设计。我要做的唯一更改是将参数从类更改为接口

然后可以有一个实现该参数的参数类(或者可能是ParameterImpl类),也可以有一个也实现参数接口的CompositeParameter类

I have a class which composes objects of a class it inherits from,
which just doesn't seem right.
这不是复合材料的定义吗

(parameter values are bit flags)
这是我要质疑的设计部分。也许更好的参数名称是FlagSet

将逐位测试隐藏在接口后面是可以的,但是用基础计算机科学中的著名解决方案来解决问题,继承似乎有些过火


复合模式的要点是,叶对象表示简单情况,复合对象表示复杂情况,客户机代码可以对这两种情况进行相同的处理。如果您的接口需要客户机代码来区分这两者,或者需要迭代基类组件,那么使用该模式并不能真正获得任何价值

例如,如果您主要关心的是测试,那么基类可以有一个方法:

bool Test() const;
叶类实现如下所示:

bool LeafTester::Test() { return _DoTest(); }
bool CompositeTester::Test() {
    bool success = true;

    for (int i = 0; i < m_count; i++)
        success &= m_components[i].Test();

    return success;
}
复合类实现如下所示:

bool LeafTester::Test() { return _DoTest(); }
bool CompositeTester::Test() {
    bool success = true;

    for (int i = 0; i < m_count; i++)
        success &= m_components[i].Test();

    return success;
}

我使用了for循环来保持示例的简单性。在实践中,我将使用STL代替.< /P>,当谈到C++代码时,使用C++代码来描述问题几乎总是更好,而不是英语。因此,CypItEngult将实现参数接口并包含参数IMPL对象?实现了参数InterfaceThet的那个或任何对象,这是复合的定义,我的问题是,它继承了它所组成的相同的东西。我的目标不是隐藏逐位测试,而是创建一些可以为测试目的生成所有可能值的东西。它是将在逐位测试中使用的GetValue(…)的结果。我在这里使用name参数只是为了说明,我真正的问题是关于组合/继承设计。组合的定义是它继承自它正在组合的同一事物。请参阅“设计模式”第164页。如果你的组合就是这样做的,那么它遵循这个模式。根据这个模式,组合和叶都来自抽象的基类组件。所以,如果你想实现书中描述的模式,参数将是抽象的,你需要第二个子类LeafParameter。我真的不明白为什么向量不能满足你的需要,但也许还有一些附加要求没有在问题中说明?好的,那么你是对的,事实证明,我提出的正是复合模式(在我的实际实现中,参数是抽象的)。是的,您的Test()示例或多或少就是我的GetValues()函数所做的。谢谢你的帮助。