简体   繁体   English

如何让类相互继承?

[英]How to make classes inherit each other?

I have two classes ClassOne , ClassTwo each one in a separated file .h and .cpp as follow:我在单独的文件.h.cpp中有两个类ClassOneClassTwo ,如下所示:

// ClassOne.h file
#include <ClassTwo.h>
class ClassOne : public ClassTwo {
protected:
    type m_string; // Required for "ClassTwo"
}

// ClassTwo.h file
#include <ClassOne.h>
class ClassTwo : public ClassOne {
public:
    method1(); // <---|
    method2(); // <- Required to be available in class ClassOne.
    method3(); // <---|
}

As you saw in the previous code the two classes inherit each other, but that code occurs errors error C2504: 'ClassOne': base class undefined and error C2504: 'ClassTwo': base class undefined .正如您在前面的代码中看到的,两个类相互继承,但该代码出现错误error C2504: 'ClassOne': base class undefinederror C2504: 'ClassTwo': base class undefined

The purpose of that is I want the member's functions of "ClassTow" to be available in "ClassOne" but also "ClassTwo" needs a member variable in "ClassOne".这样做的目的是我希望“ClassTow”的成员函数在“ClassOne”中可用,但“ClassTwo”需要“ClassOne”中的成员变量。

How to make the two classes inherit from each other?如何让两个类相互继承?

Maybe the CRTP would solve your problem.也许 CRTP 会解决你的问题。 First, create the class ClassOne as a template that uses a type ClassTwo as a parameter.首先,创建 class ClassOne作为模板,使用ClassTwo类型作为参数。 This type shall be the class that inherits from ClassOne :此类型应为继承自 ClassOne 的ClassOne

template<typename ClassTwo>
class ClassOne
{
public:
    void methodOne() {
        if (you need to use methods of ClassTwo) 
             static_cast<ClassTwo*>(this)->methodTwo();
    }
};

Then define ClassTwo as the one that inherits ClassOne specifying the template parameter with itself:然后将ClassTwo定义为继承ClassOne指定模板参数的那个:

class ClassTwo : public ClassOne<ClassTwo>
{
public:
    void methodTwo() {
        if (you need to use methods of ClassOne) 
             methodOne();
    }
};

Update: that is a hard way to solve the problem.更新:这是解决问题的艰难方法。 There are other even simpler ones, for example to use virtual methods:还有其他更简单的方法,例如使用虚拟方法:

class ClassOne
{
public:
    void methodOne() {
        if (you need to use methods of ClassTwo) 
             methodTwo();
    }
    virtual void methodTwo() = 0;
};

class ClassTwo : public ClassOne
{
public:
    void methodTwo() override {
        if (you need to use methods of ClassOne) 
             methodOne();
    }
};

Both approaches have their own pros and cons.两种方法都有各自的优缺点。 We need to know your task in details to advise which one is better.我们需要详细了解您的任务,以建议哪个更好。

How to make classes inherit each other?如何让类相互继承?

A class cannot possibly inherit its child. class 不可能继承它的孩子。 A base can only be specified in the class definition, and a class cannot be used as a base unless it has been specified.基数只能在 class 定义中指定,除非已指定,否则不能将 class 用作基数。 There is no way to order the two definitions in such way that both are before each other.没有办法以这样的方式对这两个定义进行排序,以使两者都在彼此之前。 Thus, this is not possible.因此,这是不可能的。

Such inheritance would also be paradoxical because a class object contains the base as a sub object within itself.这样的 inheritance 也将是自相矛盾的,因为 class object 包含作为子 ZA8CFDE6331BD4B62AC96F8911 的基础。 As such, you would end in situation where there is One inside Two inside One inside Two inside One inside Two inside One inside Two inside One inside Two inside One inside Two inside... can you see where this is going?因此,你会以一内二内一内二内一内二内一内二内一内二内一内二内的情况结束……你能看到这是怎么回事吗? It is going nowhere.它无处可去。 The object is infinitely large because it contains infinitely deep inheritance hierarchy. object 无限大,因为它包含无限深的 inheritance 层次结构。

To express this in another way: Inheritance hierarchy can be seen as a directed graph.用另一种方式表达这一点:Inheritance 层次结构可以看作是有向图。 An inheritance hierarchy graph cannot contain a cycle. inheritance 层次图不能包含循环。


The purpose of that is I want the member's functions of "ClassTow" to be available in "ClassOne" but also "ClassTwo" needs a member variable in "ClassOne".这样做的目的是我希望“ClassTow”的成员函数在“ClassOne”中可用,但“ClassTwo”需要“ClassOne”中的成员变量。

You can have two classes, both of which having access to all of the data members and the functions like this:您可以有两个类,它们都可以访问所有数据成员和如下函数:

struct ClassOne {
    // all the member functions
    // all the data members
};

struct ClassTwo : ClassOne {
    // nothing here
};

Now you have two classes, both of which have access to the same data members and member functions.现在您有两个类,它们都可以访问相同的数据成员和成员函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM