如何编写 class 的基 child class 相互依赖?

How to write a base class whose child classes depend on each other?

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

//--------------------------------------------------------
//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
}

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

//--------------------------------------------------------
//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
    }
}