Call derived class non virtual member functions from base class pointer

Viewed 4866

We know that, derived class members functions can be accessed through a base class pointer in C++ , provided that these member functions have to be virtual. Is there a means to access derived class member functions which are NOT virtual or pure virtual from base class pointer.

i.e. I want to call derived class member functions which are present only in derived class & not in base class through base class pointer. How would I achieve this?

For example, if I design a factory design pattern,

class Vehicle {
public:
    virtual void printVehicle() = 0;
    static Vehicle* Create(VehicleType type);
};
class TwoWheeler : public Vehicle {
public:
    void printVehicle() {
        cout << "I am two wheeler" << endl;
    }
    void Some2WheelerONLYSpecificOPeration()
    {

    }
};
class ThreeWheeler : public Vehicle {
public:
    void printVehicle() {
        cout << "I am three wheeler" << endl;
    }
void Some3WheelerONLYSpecificOPeration()
    {

    }
};
class FourWheeler : public Vehicle {
    public:
    void printVehicle() {
        cout << "I am four wheeler" << endl;
    }
void Some4WheelerONLYSpecificOPeration()
    {

    }
};

// Factory method to create objects of different types.
// Change is required only in this function to create a new object type
Vehicle* Vehicle::Create(VehicleType type) {
    if (type == VT_TwoWheeler)
        return new TwoWheeler();
    else if (type == VT_ThreeWheeler)
        return new ThreeWheeler();
    else if (type == VT_FourWheeler)
        return new FourWheeler();
    else return NULL;
}

int main()
{
Vehicle* basePtr =   Vehicle::Create(VT_TwoWheeler);
basePtr->Some2WheelerONLYSpecificOPeration();   //HOW TO ACHIEVE THIS CALL

basePtr =   Vehicle::Create(VT_ThreeWheeler);
basePtr->Some3WheelerONLYSpecificOPeration(); //HOW TO ACHIEVE THIS CALL

basePtr =   Vehicle::Create(VT_FourWheeler);
basePtr->Some4WheelerONLYSpecificOPeration(); // //HOW TO ACHIEVE THIS CALL
}
3 Answers

I'm trying to find the best way to use polymorphism without using inheritance, because I want to avoid virtual calls. I was looking for a way to improve what I currently have (with no avail) and I stumbled on this question. This is the best I can do so far:

template<class VehicleDetails>
class Vehicle {
    VehicleDetails details;

public:
    VehicleDetails& getDetails() {
        return details;
    }

    const VehicleDetails& getDetails() const {
        return details;
    }

    void printDetails() const {
        details.printDetails();
    }
}

class TwoWheeler {
public:
    void printDetails() const {
        cout << "I am two wheeler" << endl;
    }

    void specificTwoWheelerMethod() const {
        cout << "I am specific functionality" << endl;
    }
}

Then you use it as such:

Vehicle<TwoWheeler> vehicle;
vehicle.printDetails(); // prints "I am two wheeler"

Unfortunately this complicates things. Now every class/struct or function that takes a vehicle must be templated, unless you know the type of vehicle.

template<class VehicleDetails>
void doGeneralVehicleThings(const Vehicle<VehicleDetails>& vehicle) {
    // ...
}

On the plus side when you do know the type you can access specific functionality via the getDetails() method without any casting or runtime overhead involved:

void doTwoWheelerThings(const Vehicle<TwoWheeler>& twoWheelerVehicle) {
    twoWheelerVehicle.getDetails().specificTwoWheelerMethod(); // prints "I am specific functionality"
}
Related