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:
- Casting (maybe not ugly for some), is
std::static_pointer_castfast? 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". - Implement all member function needed in
BaseConsumablewith a throw, this is ugly, also it does increase the memory allocated to theBaseConsumable. - Use
boost::anyand haveBaseConsumablehas a map to the attributes, and process the map usingIOnlyCareAboutCommunOpas 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).