Avoid Casting when Consumer Function takes a Base Class

30 Views Asked by At

How can do the below commented line please i.e. "//How to do this without casting?". What I mean is how to do it by changing my design, I know I can't do it this way. Some ugly ways I thought about:

  1. Casting (maybe not ugly for some), is std::static_pointer_cast fast? Is it compile time or run time operation, also can I do it without a lot of ifs i.e. please remember the comment: "//I can't be a template and there are a lot of me".
  2. Implement all member function needed in BaseConsumable with a throw, this is ugly, also it does increase the memory allocated to the BaseConsumable.
  3. Use boost::any and have BaseConsumable has a map to the attributes, and process the map using IOnlyCareAboutCommunOp as it know what to look for.
#include <iostream>

class BaseConsumable //I can't be a template and there are a lot of me
{
public:
    virtual ~BaseConsumable() = default;
};

class Consumable : public BaseConsumable
{
public:
    virtual ~Consumable() = default;
    virtual void IamSomethingCommunBetweenChilds() const = 0;

protected:
    Consumable() = default;
};

class ConsumableTypeOne : public Consumable
{
public:
    ConsumableTypeOne() : Consumable() {};
    virtual void IamSomethingCommunBetweenChilds() const override
    {
        std::cout << "Im implemented" << std::endl;
    }

    virtual void ImSomthingSpecialOne() const
    {
        std::cout << "I am special One" << std::endl;
    }
};

class ConsumableTypeTwo : public Consumable
{
public:
    ConsumableTypeTwo() : Consumable() {};
    virtual void IamSomethingCommunBetweenChilds() const override
    {
        std::cout << "Im implemented" << std::endl;
    }

    virtual void ImSomthingSpecialTwo() const
    {
        std::cout << "I am special Two" << std::endl;
    }
};


class IOnlyConsumeBaseConsumable
{
public:
    void IOnlyCareAboutCommunOp(const std::shared_ptr<const BaseConsumable>& ImConsumableAndTheyAreLotsOfMe) const
    {
        //ImConsumableAndTheyAreLotsOfMe->IamSomethingCommunBetweenChilds(); //How to do this without casting?
    };
};

Could you please help me if you have come accross this, or this is exactly why casting exists in the first place? (I do care about speed).

0

There are 0 best solutions below