Creating queue of objects derived from base class in c++

445 views Asked by At

So, in short, I am writing a rather simple calculator (for reverse Polish notation). My base class is Symbol, it is a pure virtual class, and I have bunch of other classes:

Operand: inherits from Symbol
Number: inherits from Operand
Constant: inherits from Operand
Variable: inherits from Operand

Function: inherits from Symbol

Now i want to parse a string into queue/vector of objects, while still being able to use their individual methods. I had lots of failed attempts, and came across object slicing which, I think happens in my case.

std::queue<Symbol*> parse(std::string s){
//split is custom function for converting string -> (ListOf string) without whitespaces
std::queue<std::string> StringExps = split(s);
std::queue<Symbol*> Q;

while(!StringExps.empty()){
    std::string expr = StringExps.front();
    StringExps.pop();
    // now i want to be able to use get_val, from Number class, this throws an exception
    if( '0' <= expr[0] && expr[0] <= '9'){
        Q.push(new Number(std::stoi(expr)));
        std::clog<<Q.front()->get_val()<<"\n";
    }
    //checking if expr is a valid function ("min" "+" "sin" etc.)
    else if(checkif_function(expr)){
        Q.push(new Function(expr));
        std::clog<<Q.front()->return_type()<<"\n";
        //std::clog<<"Funtion/operator\n";
    }
    else if(checkif_const(expr)){
        Q.push(new Const(expr));
    }
}
return Q;}

Now I need to be able to use specific methods from each derived class, but this does not work, and I have no idea how to fix it. Could anyone help me with that?

0

There are 0 answers