1

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?

Kombajn
  • 121
  • 2
  • 2
    "this does not work". Could be more specific please, this is unclear. – kebs Jun 12 '22 at 21:42
  • 1
    If you can't make the functions `virtual` and `override` them in the derived classes and you don't know what the derived type is, you can try to `dynamic_cast` the base class pointer to each of the derived classes (from most derived and back). – Ted Lyngmo Jun 12 '22 at 21:42
  • 1
    There's no object slicing in the shown code. However, what does "this does not work" mean? Please be specific, and explain your question, succinctly. – Sam Varshavchik Jun 12 '22 at 21:48
  • 2
    `std::queue Q;` -> `std::queue> Q;`, if you want to avoid the misery of memory leaks and double deletes. – Paul Sanders Jun 12 '22 at 22:14
  • 1
    You can't call "specific" methods from a derived class through a base class pointer. That wouldn't make any sense. You need a derived class pointer or reference. You *can* `dynamic_cast` the type down, but this is often a sign of poor design. If sensible, use polymorphism. If you really *need* a `Number`, then you probably shouldn't be storing it as a `Symbol`. If you really *must* store everything in one place, you can use [variants](https://en.cppreference.com/w/cpp/utility/variant). But a custom recursive data structure (e.g., some custom tree structure) might make more sense. – Alexander Guyer Jun 12 '22 at 23:09
  • What i ment by "this does not work" is for example `Q.front()->get_val()` throws error, since `Symbol` class has no member "get_val" (`Number` class does). In this case, I want to store(rather my professor wants me to) all the objects of derived classes in a `std::queue` of base class (`Symbol`) type. I get that i cannot do that, I just dont really know how to make it work. – Kombajn Jun 13 '22 at 07:20
  • Dont add details on your question on the comments! Instead, clic the "edit" button and add your clarifications to the question. – kebs Jun 13 '22 at 21:42

0 Answers0