C ++ - accessing a subclass member of an abstract class pointer vector

The error is here:

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

      

When I want to access getThrust()

from a class Engine

by vector<RocketPart*> rocket

using rocket[i]->getThrust()

, I get an error from the top of my question. What am I doing wrong? Can I access it via rocket [index] -> getThrust ()?

+3


source to share


5 answers


The error you received is correct: RocketPart

there is no named function in the class getThrust

. You can:

  • add virtual float getThrust()

    to class RocketPart

    . This may not be a good solution because not everyone RocketPart

    can contain cravings; however you could just return 0

    from it, or make it a pure virtual function (which would mean you no longer have to create an objectRocketPart

  • dynamic_cast

    your object RocketPart

    in Engine

    . This can be done as follows:

    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 use any RocketPart*

that is notEngine

+1


source


The class RocketPart

has no function getThrust()

inside it, so your vector calling the function is not working. If getThrust()

is a normal procedure that will be used in classes inheriting from it, you must add it as a virtual function to the base class as follows:



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

    virtual float getThrust();

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

      

+2


source


You need to add

virtual float getThrust() = 0;

      

In your class RocketPart

. Also, I 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 fine:

class Engine : public RocketPart {

      

I would also suggest looking at memory allocation - if the source code of the sample is not just an example (are you leaking objects Engine

, maybe a vector<shared_ptr<Engine>>

better idea?)

0


source


this is because the type stored in the container is RocketPart. You need to lower the pointer to "Engine". Therefore, 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 the throw: if the rocket [i] is not an engine, the program will most likely crash.

0


source


The getThrust () method is defined in the Engine class, not in the RocketPart. You need to move the method in the base RocketPart class in order to access it by vector. Alternatively, you can also use dynamic transform to "transform" the RocketPart into an engine.

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

      

Note that using dynamic_cast often means bad design.

0


source







All Articles