简体   繁体   English

C ++访问抽象类指针向量的子类成员

[英]C++ accessing member of subclass of abstract class pointer vector

Error over here: 错误在这里:

Rocket.cpp:31:16: error: no member named 'getThrust' in 'RocketPart'
    rocket[0]->getThrust();

When I want to access getThrust() from class Engine over the vector<RocketPart*> rocket with rocket[i]->getThrust() , I get the error message from the top of my question. 当我想通过带有rocket[i]->getThrust()vector<RocketPart*> rocket从类Engine访问getThrust() ,我从问题的顶部得到错误消息。 What am I doing wrong? 我究竟做错了什么? Is it possible to access it over rocket[index]->getThrust()? 是否可以通过rocket [index] - > getThrust()访问它?

The class RocketPart does not have a function getThrust() inside of it, which is why your vector calling the function doesn't work. RocketPart没有函数getThrust() ,这就是你调用函数的向量不起作用的原因。 If getThrust() is a common routine that will be used in the classes inheriting from it, you should add it as a virtual function in the base class like so: 如果getThrust()是将在从它继承的类中使用的常用例程,则应将其作为虚函数添加到基类中,如下所示:

class RocketPart {
public:
    RocketPart();
    RocketPart(const RocketPart& orig);
    virtual ~RocketPart();

    virtual float getThrust();

    virtual void print() = 0;
protected:
    // some members
};

The error you receive is correct: there is no function called getThrust in the class RocketPart . 您收到的错误是正确的: getThrust类中没有名为getThrustRocketPart You can either: 你可以:

  • add a virtual float getThrust() to your RocketPart class. 将一个virtual float getThrust()添加到您的RocketPart类。 This may not be a good solution, because not all RocketPart could contain thrust; 这可能不是一个好的解决方案,因为并非所有的RocketPart都能包含推力; however, you could simply return 0 from it, or make it a pure virtual function (which would mean you are no longer to ever create a RocketPart object 但是,你可以简单地从它return 0 ,或者使它成为一个纯虚函数(这意味着你不再需要创建一个RocketPart对象
  • dynamic_cast your RocketPart object into an Engine . dynamic_cast你的RocketPart对象到Engine This can be done with the following: 这可以通过以下方式完成:

    for (size_t r = 0; r < rocket.size(); ++r) { if (const Engine* engine = dynamic_cast<Engine*>(rocket[r])) { // able to successfully convert this RocketPart into an Engine engine.getThrust(); } }

You will not be able to cast any RocketPart* that isn't an Engine 您将无法投射任何非Engine RocketPart*

You need to add 你需要添加

virtual float getThrust() = 0;

In your RocketPart class. 在你的RocketPart类中。 Besides, I can see absolutely no reason to use virtual inheritance (this is only needed if you inherit from multiple classes and you have The Diamond Problem ), so instead: 此外,我完全没有理由使用虚拟继承(只有从多个类继承并且你有钻石问题时才需要这个),所以相反:

class Engine : virtual public RocketPart {

you should be ok with just : 你应该只是:

class Engine : public RocketPart {

I would also suggest looking at your memory allocation(s) - if your sample source code is not just an example (you are leaking Engine objects, maybe vector<shared_ptr<Engine>> would be a better idea ?) 我还建议查看你的内存分配 - 如果你的示例源代码不只是一个例子 (你正在泄漏Engine对象,也许vector<shared_ptr<Engine>>会更好吗?)

it is because the type stored in the container is RocketPart. 这是因为存储在容器中的类型是RocketPart。 You need to downcast the pointer to Engine. 您需要向下转换指向Engine的指针。 So instead of 而不是

rocket[0]->getThrust();

you need to write: 你需要写:

Engine *pPart = (Engine *) rocket[i]; //you can use also static_cast<> or dynamic_cast<>
pPart->getThrust();

Be careful when using cast: if rocket[i] is not an Engine, the program will likely crash. 使用演员时要小心:如果rocket [i]不是引擎,程序可能会崩溃。

The method getThrust() is defined in the Engine class not in the RocketPart. getThrust()方法在Engine类中定义,而不是在RocketPart中定义。 You need to move the method in the base class RocketPart, for access it over the vector. 您需要在基类RocketPart中移动该方法,以便通过向量访问它。 Alternatively you can also use a dynamic cast for "convert" the RocketPart to the Engine. 或者,您也可以使用动态强制转换将RocketPart“转换”为引擎。

Engine* engine = dynamic_cast<Engine>(rocket[index]);
if (engine) {
    engine->getThrust();
}

Consider that the use of dynamic_cast often means bad design. 考虑使用dynamic_cast通常意味着糟糕的设计。

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

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