简体   繁体   English

C ++基类是否可能具有带有派生类的参数的函数

[英]C++ Is it possible that the base class has a function with a parameter of the derived class

I want to do essentially the following: 我基本上要执行以下操作:

class Base{
     void dosomestuff(Derived instanceOfDerived)
     {
         //Something
     }
};

class Derived : public Base{
     //Something
};

The Base needs a include of Derived, but to declare Derived it needs the declaration of Base first. Base需要包含Derived的包含,但是要声明Derived,它需要首先声明Base。 Forward declaration does not work, because I do not want to use pointers. 前向声明不起作用,因为我不想使用指针。

Now my question: How do I accomplish that without pointers? 现在我的问题是:如何在没有指针的情况下完成此任务? Is that even possible? 那有可能吗? Or is the only possibility to make instanceOfDerived a pointer? 还是使instanceOfDerived成为指针的唯一可能性?

You better do something like: 您最好执行以下操作:

class Derived;
class Base{
     void dosomestuff(Derived& instanceOfDerived)
     {
         //Something
     }
};

class Derived : public Base{
     //Something
};

I would even move the body of method dosomestuff to the source cpp code where you can include derived.h as well as base.h dosomestuff至将方法dosomestuff移到源cpp代码中,您可以在其中包含derived.h和base.h

base.h could look like: base.h可能看起来像:

#ifndef BASE_H_
#define BASE_H_

class Derived;
class Base{
     void dosomestuff(const Derived& instanceOfDerived);
};

#endif

derived.h: derived.h:

#ifndef DERIVED_H_
#define DERIVED_H_

#include "base.h"

class Derived : public Base{
     //Something
};

#endif

base.cpp: base.cpp:

#include "base.h"
#include "derived.h"

void Base::dosomestuff(const Derived &instanceOfDerived) {
    //Something
}

The original version of the question asks about having Base hold Derived as a data member. 该问题的原始版本询问有关将Base Hold作为数据成员Derived的问题。 That's obviously impossible. 这显然是不可能的。 It's the same infinite recursion problem (a Derived contains a Base subobject, so a Base would recursively contain itself, and it will be turtles all the way down.) 这是相同的无限递归问题(“ Derived对象”包含Base子对象,因此Base会递归包含自身,并且一直都是乌龟。)

The revised question asks about having a member function of Base taking a by-value argument of type Derived . 修改后的问题询问有关具有Base的成员函数并采用Derived类型的按值参数的问题。 That's perfectly possible. 那完全有可能。 You need a forward declaration of Derived , and to defer the definition of the member function until after the actual definition of Derived : 您需要一个Derived的前向声明,并将成员函数的定义推迟到Derived的实际定义之后:

class Derived;
class Base{
     void dosomestuff(Derived instanceOfDerived); // declaration only
};

class Derived : public Base{
     //Something
};

// out-of-class definition, so making it explicitly inline
// to match the in-class definition semantics
inline void Base::dosomestuff(Derived instanceOfDerived) {
     //Something
}

Demo . 演示

That means your inheritance model is not make sense if you really want to do that. 这意味着如果您确实想这样做,那么您的继承模型就没有意义。 Remember "Make sure public inheritance models 'is a.' 请记住,“确保公共继承模型是”。 " http://debian.fmi.uni-sofia.bg/~mrpaff/Effective%20C++/EC/EI35_FR.HTM If you make them have inheritance relationship, that means Derived is a Base, instead of "Derived is part of Base". http://debian.fmi.uni-sofia.bg/~mrpaff/Effective%20C++/EC/EI35_FR.HTM如果您使它们具有继承关系,则意味着Derived是Base,而不是” Derived是Base的一部分”。 You can create 2 classes to do that instead of making them have inheritance relationship. 您可以创建2个类来做到这一点,而不是使它们具有继承关系。

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

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