【问题标题】:How to write a base class whose child classes depend on each other? [duplicate]如何编写一个子类相互依赖的基类? [复制]
【发布时间】:2019-12-27 05:38:21
【问题描述】:

我正在尝试编写一个数学集合类。我的想法是创建一个名为 Set 的基类。然后我写了 Set 类的两个子类。即FiniteSet 类和CountableSet 类(可以处理无限集)。我现在的问题是这些子类相互依赖,我无法解决这个问题。我也会欣赏这个问题的完全不同的解决方案。

//--------------------------------------------------------
//Set class
//--------------------------------------------------------
class Set
{
public:
    //some virtual functions

protected:
    //some attributes 
};

//--------------------------------------------------------
//FiniteSet class
//--------------------------------------------------------
class FiniteSet : public Set
{
public:
    //implements all virtual functions 

    //function which needs to know CountableSet:
    Set unionWith(Set* otherSet)
    {
        if(typeid(CountableSet) != typeid(*otherSet))
        {
            //the other set is finite. We can simply add all             
                        //elements from otherSet to this set.
        }
        else
        {
            //create a CountableSet and return it
        }
    }

private:
    //some attributes
}

//--------------------------------------------------------
//CountableSet class
//--------------------------------------------------------
class CountableSet : public Set
{
public:
    //implements all virtual functions 

    //function which needs to know FiniteSets
    Set intersectWith(Set* otherSet)
    {
        if(typeid(FiniteSet) == typeid(*otherSet))
        {
            //do something and return FiniteSet
        }
        else
        {
            //do something and return occasionally CountableSet
        }
    }

private:
    //some attributes
}

【问题讨论】:

  • 顺便说一句,您可以使用double dispatch 来避免typeid
  • 这个问题/答案提高了头文件的重要性;)。

标签: c++ inheritance design-patterns dependencies multiple-inheritance


【解决方案1】:

在定义类之前声明它们,例如:

//--------------------------------------------------------
//Set class
//--------------------------------------------------------
class Set
{
public:
    //some virtual functions

protected:
    //some attributes 
};

//--------------------------------------------------------
// Declare FiniteSet class
//--------------------------------------------------------
class FiniteSet : public Set
{
public:
    //implements all virtual functions 

    //function which needs to know CountableSet:
    Set unionWith(Set* otherSet);

private:
    //some attributes
};

//--------------------------------------------------------
// Declare CountableSet class
//--------------------------------------------------------
class CountableSet : public Set
{
public:
    //implements all virtual functions 

    //function which needs to know FiniteSets
    Set intersectWith(Set* otherSet);

private:
    //some attributes
};

// Define FiniteSet::unionWith
Set FiniteSet::unionWith(Set* otherSet);
{
    if(typeid(CountableSet) != typeid(*otherSet))
    {
        //the other set is finite. We can simply add all             
                    //elements from otherSet to this set.
    }
    else
    {
        //create a CountableSet and return it
    }
}

// Define CountableSet::intersectWith
Set CountableSet::intersectWith(Set* otherSet)
{
    if(typeid(FiniteSet) == typeid(*otherSet))
    {
        //do something and return FiniteSet
    }
    else
    {
        //do something and return occasionally CountableSet
    }
}

【讨论】:

  • 这工作完美!
猜你喜欢
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
相关资源
最近更新 更多