Jump to content

C++ Void Pointers

JoeTheSmiter

Hello all. I'm working on an assignment for my Data Structures class and we have to create a RPN calculator. So, we have to store ints, doubles, and the operators in a queue. He says to use void pointers for this, however I can't figure out how to know what type the data should be when I pull them from the queue to do the calculations. Thanks in advanced.

Link to comment
Share on other sites

Link to post
Share on other sites

I wont necessarily tell you how to do it, but think about how you might be able to store the type information alongside each pointer.


Edit:  As a minor aside since it's a data structures class, RPN is processed in a stack, not a queue.

Edited by Yamoto42
Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, JoeTheSmiter said:

He says to use void pointers for this

Sounds like poor teaching to me. One would frown on a solution like that even in plain C. C++ allows for much better ways to handle something like this without sacrificing type information, which is akin to sacrilege.

 

Anyhow, since you've been told to do it with void pointers. Consider creating a struct that holds a void pointer and a enum for the type. You then store these structs in your queue.

Link to comment
Share on other sites

Link to post
Share on other sites

Here's an expert from the assignment.

Quote

Your program must have the following classes:

  • Parser, a class that "figures out" what the user entered and prepares three things: an operand stack, an operator stack, and a queue which holds the expression that needs to be translated from algebraic to RPN. (NOTE: This implies that the queue must hold different data types -- operands, parens, and operators)
  • A queue class which accepts tokens from the parser. Tokens can be either a decimal number, integer,  an operator, or a parenthesis.  (You cannot "cheat" by having a queue of C++ strings. The queue must "legitimately" hold tokens of different types. Hint: pointers to type void)
  • Two instances of the stack class. One which holds operators, the other holds integers or doubles.

You MAY NOT use the STL stack or queue for this, as in you are being tested on your ability to write their equivalents. You must write the appropriate stacks and queues. The stacks and queues must be as generic as possible; meaning that I should be able to use any other students stack or queue in your program and have it work correctly.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×