Jon Abraham
Jon Abraham

Reputation: 965

How to create a stack of different types of variable?

Can someone help me how to create a stack that handles different types of variable. I have a BNF grammar that i need to push into stack and below are the things that i need to push into stack

1) +,-,/,*

2) integers - 0,1,2..9

3) characters - a,b..z,A,B..Z

4) $ Sign, Parenthesis (open, close), underscore

I am not sure if my below attempt would satisfy all of the above. Please correct me if i am wrong.

stack<int,string>mystack

Upvotes: 0

Views: 76

Answers (1)

Edward Strange
Edward Strange

Reputation: 40887

You need to use polymorphism and push dynamic types onto your stack. This can either be in the form of something like boost.variant, or a simple class hierarchy and store the items in the stack by smart pointer. That is not an exhaustive list.

C++ doesn't support, as a core language item, dynamic variables. You create them using techniques like virtual functions and inheritance, or type erasure.

Upvotes: 3

Related Questions