C ++ Call function from an instance of a base class

I am new to C ++, but I ran into a problem that I cannot solve. I will use cars to illustrate the problem, just to make things simpler. So, let's say that I have a base class car, and I have different brands that inherit from this class. For instance:

class Car { public: Car(); }; class Ford: public Car { public: Ford(); void drive(); void park(); }; 

The whole idea is to combine all these different machines into a single vector of type Car. For instance:

 vector<Car*> cars; cars.push_back(new Ford()); cars.back()->drive(); //this won't work 

How can I call a derived function on an instance of a base class? Please note that I want to place all this in one vector. The reason for this is because I want to use only the most recently added instance of the car class. (In this case, the derived car class is ford). Also note that all car classes will have the same features.

+6
source share
6 answers

If these functions are indeed common to all derived classes, then you have a common interface, so you must express this through the base class. To do this, you declare these functions purely virtual:

 class Car { public: virtual void drive() = 0; // Pure-virtual function }; class Ford : public Car { public: virtual void drive() { std::cout << "driving Ford\n"; } }; ... vector<Car*> cars; cars.push_back(new Ford()); cars.back()->drive(); //this will work 

[On the side of a note, it is generally believed that bad practice has a vector of source pointers because it makes memory management more difficult. You should consider using smart pointers.]

+7
source

You have two options: either put the virtual disk method () in the Car Car definition, or point the Car pointers to the Ford pointers. Most likely, you will want to do the first.

 class Car { public: Car(); virtual void drive() { // default implementation} }; 

Now you can drive () your car! You can also make disk () a pure virtual function, for example:

 class Car { public: Car(); virtual void drive() = 0; }; 

This basically means that there is no default implementation for disk (): it MUST be overridden in a subclass. The second method that I mentioned, which, again, you probably do not want, but should be included for completeness, is to overlay the pointer:

 static_cast<Ford*>(cars.back())->drive(); 

This only works if you know in advance that the car is a Ford, but there is little use in this scenario. You can also look at dynamic_cast.

+3
source

If all Car classes have the same functions, declare them as pure virtual in the base Car class:

 class Car { public: Car(); virtual ~Car(); virtual void drive() = 0; virtual void park() = 0; }; 

This will allow you to use the example code that vector uses to work as published.

+2
source

Perhaps, if possible, you can define the base class as

 class Car { public: Car(); virtual void drive(); }; 
0
source

You must define an interface, for example:

 class Car{ public: Car(); virtual void moveForward(unsigned int speed) = 0; virtual void moveBack(unsigned int speed) = 0; //... virtual ~Car(); }; 

Do not forget to make a virtual destructor. After that, you just need to implement these methods in your child classes and call them after. Also in the vector, you can use shared_ptr or just pass instances directly.

0
source

You have to put a virtual function.

A virtual function in the base class. A virtual function can be implemented in a subclass, so you can specialize the behavior of the disk (), for example.

Here you can find faq (or tutorial) about virtual functions:

http://www.parashift.com/c++-faq-lite/virtual-functions.html

0
source

Source: https://habr.com/ru/post/912224/


All Articles