简体   繁体   English

在C++中将派生类指针分配给基类指针

[英]assigning derived class pointer to base class pointer in C++

I have following我有以下

class base
{
};

class derived : public base
{
  public:
   derived() {}
   void myFunc() { cout << "My derived function" << std::endl; }

};

now I have我现在有

base* pbase = new derived();
  pbase->myFunc();

I am getting error myFunc is not a member function of base.我收到错误 myFunc 不是 base 的成员函数。

How to avoid this?如何避免这种情况? and how to make myFunc get called?以及如何让 myFunc 被调用?

Note I should have base class contain no function as it is part of design and above code is part of big function注意我应该让基类不包含任何功能,因为它是设计的一部分,上面的代码是大功能的一部分

myfunc needs to be accessible from the base class, so you would have to declare a public virtual myfunc in base . myfunc需要可从基类访问,因此您必须在base声明一个公共虚拟myfunc You could make it pure virtual if you intend for base to be an abstract base class, ie one that cannot be instantiated and acts as an interface:如果您打算将base设为抽象基类,即无法实例化并充当接口的类,则可以将其设为纯虚拟:

class base
{
 public:
  virtual void myfunc() = 0; // pure virtual method
};

If you ant to be able to instantiate base objects then you would have to provide an implementation for myfunc :如果您希望能够实例化base对象,那么您必须为myfunc提供一个实现:

class base
{
 public:
  virtual void myfunc() {}; // virtual method with empty implementation 
};

There is no other clean way to do this if you want to access the function from a pointer to a base class.如果您想从指向基类的指针访问函数,则没有其他干净的方法可以做到这一点 The safetest option is to use a dynamic_cast最安全的选择是使用dynamic_cast

base* pbase = new derived;

....
derived* pderived = dynamic_cast<derived*>(pbase);
if (derived) {
  // do something
} else {
  // error
}

If you are adamant that this function should NOT be a part of base, you have but 2 options to do it.如果您坚持认为此功能不应该是 base 的一部分,那么您只有 2 个选项可以执行此操作。

Either use a pointer to derived class要么使用指向派生类的指针

derived* pDerived = new derived();
pDerived->myFunc();

Or ( uglier & vehemently discouraged ) static_cast the pointer up to derived class type and then call the function或者(更丑陋强烈不鼓励)将指针 static_cast 指向派生类类型,然后调用该函数
NOTE : To be used with caution .注意:要谨慎使用 Only use when you are SURE of the type of the pointer you are casting, ie you are sure that pbase is a derived or a type derived from derived .仅当您确定要转换的指针的类型时才使用,即您确定pbasederived或派生自derived的类型。 In this particular case its ok, but im guessing this is only an example of the actual code.在这种特殊情况下没问题,但我猜这只是实际代码的一个例子。

base* pbase = new derived();
static_cast<derived*>(pbase)->myFunc();

To use the base class pointer, you must change the base class definition to be:要使用基类指针,您必须将基类定义更改为:

class base
{
public:
    virtual void myFunc() { }
};

I see no other way around it.我没有其他办法解决它。 Sorry.对不起。

You could add it as a member of base and make it a virtual or pure virtual function.您可以将其添加为 base 的成员并使其成为虚拟或纯虚拟函数。 If using this route however, you should also add a virtual destructor in the base class to allow successful destruction of inherited objects.但是,如果使用此路由,您还应该在基类中添加一个虚拟析构函数以允许成功销毁继承的对象。

class base
{
public:
   virtual ~base(){};
   virtual void myFunc() = 0;
};

class derived : public base
{
  public:
   derived() {}
   void myFunc() { cout << "My derived function" << std::endl; }

};

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

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