Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stack that can handle both strings and floats in same class??

Status
Not open for further replies.

hunternjb

Programmer
Mar 21, 2001
7
0
0
US
I am having a problem trying to come up with a way to build a stack that can handle both strings and floats in the same class. I am assuming a max of 5 elements in the stack to start with. I am also trying to avoid using malloc() and new when declaring the string, and using fixed arrays instead. Below is what I am pushing to the stack.

s.push( 1.0 );
s.push( 2 );
s.push( "three" );
s.push( 4 );
s.push( "FIVE" );
s.push( 6.6);

cout << s.pop() << endl;

I can't figure out a way to do this. Can someone please help me. Thanks.
 
Code:
class S {
  enum sTypes {
    STRING,
    FLOAT,
    INT
};
  struct stack {
    int type;  // integer, float or string
    void* data;
};
  struct stack myStack[5];
  void push(const char* str);
  void push(int* nr);  // pass the pointer to the stack
  void push(double* nr);
...
  
};

S::push(const char*str)
{ myStack[index].type = STRING;
  myStack[index].data = (void *)str;
  index++;
}
S::push(double* nr)
{  myStack[index].type = FLOAT;
   myStack[index].data = (void*)nr;
  index++;
}

S::pop()
{  switch (myStack[index].type)
   { case STRING...do something...
    etc...
   }
}
The appropriate function is chosen at link time.
This might be a solution.
HTH
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top