Q:

How cast an interface object to a specific inherited object

I have an interface Class : 
Class IOperand
{
    virtual ~IOperand() {};

    virtual std::string  getType() const = 0;
}

And i have several inherited Class like this one :
class Int8: public IOperand
{
    public:
      Int8(int8_t _value = 0);
      virtual ~Int8() {};
      virtual std::string getType() const;

      int8__t  getValue() const;

    private:
      int8_t _value
}

I use pointer on IOperand type, but i need to use the getValue() member function.
How can I cast an IOperand type object in a sub-class type object depending of the return of getType() (which return a string contaning the name of the target sub-class.) ?

A:

You are asking to cast a base type to a derived type. Usually this kind of casting is a sign of a poor design, since -- in most cases -- the base type should provide a virtual method that provides the desired operation. However, in some cases, it is necessary to cast to a specific class. There are really two ways to do this.
Unknown runtime type
If you do not know whether the type is or is not an Int8, then you need a polymorphic down cast. This is done with the special dynamic_cast mechanism like this:
IOperand* operand = // ...
Int8* casted = dynamic_cast<Int8*>(operand);
if (casted == nullptr) {
   // runtime type was not an Int8
   return;
}
// operate on casted object...

Known runtime type
If you absolutely know for certain that the type is the subtype, then you probably want to use a static_cast. Note, however, that a static_cast won't do the checking that dynamic_cast does. It will, however, be significantly faster to do a static_cast, since it doesn't require any reflection or walking the inheritance tree.

