#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Greece
    Posts
    63
    Rep Power
    12

    Which Child class will be poped out of the stack?


    I have a Parent class (with 3 childs) and im using data structures (eg STACKS) when pushing in the stack (the child) its obvious that "the programm" knows wich child is pushed in, so the general daclaration is of Parent class. But when poping out i cant use the Parent class cause we dont know wich child is at the top of the stack, how should the code be?

    class Parent {...}
    class Child1 {...} : public Parent
    class Child2 {...} : public Parent
    class Child3 {...} : public Parent

    class Node
    {
    private:
    Card data;
    Node *nxt;
    public:
    Node(Card,Node *);
    ~Node(void);
    Card getdata(Node *);
    Node *getnxt(Node *);
    friend class Stack;
    friend class Card;
    };

    Node::Node(Card d,Node *next)
    { data=d; nxt=next; }
    Node::~Node(void) {}
    Card Node::getdata (Node *p)
    { return(p->data); }
    Node *Node::getnxt(Node *p)
    { return(p->nxt); }

    class Stack
    {
    private:
    Node *top;
    public:
    Stack(void);
    ~Stack(void);
    Stack &push(Card);
    Card pop(void);
    bool isempty(void);
    friend class Node;
    };

    Stack::Stack(void) { top=0; }
    Stack::~Stack(void)
    { while(!isempty()) popst();}
    bool Stack::isempty(void)
    { return (top==0);}
    Stack &Stack::push(Card c)
    { top=new Node(c,top);
    return *this; }
    Card Stack::pop(void)
    {
    Card i;
    Node *p;
    i=top->data;
    p=top;
    top=top->nxt;
    delete(p);
    return(i);
    }

    main ()
    {
    Stack s;
    Child1 c1;
    Child2 c2;

    s.push(c1);//thats OK
    s.push(c2);//thats OK

    c1=s.pop();//thats NOT OK
    //its obvious that c1!=c2
    }
    Last edited by tony825; January 31st, 2003 at 05:07 PM.
    No sign
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    Why not add a class type in the base class (parent), so that when popping it, you can assign it to a variable of the base class. Then determine the class type and cast it accordingly. Of course, if you already have RTTI information in your base class, there's no need to put a class type.
    Code:
    Stack s;
    Child1 c1;
    Child2 c2;
    Parent p;
    
    s.push(c1);//thats OK
    s.push(c2);//thats OK
    
    p = s.pop();
    if (p.type == "c1")
       c1 = dynamic_cast <Child1> p;
    else if (p.type == "c2")
       c2 = dynamic_cast <Child2> p;
    Hope this helps!
    Last edited by Scorpions4ever; February 2nd, 2003 at 12:43 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Greece
    Posts
    63
    Rep Power
    12

    Thank you


    thank you for the reply, was getting worried if my question wasnt set clearly, or was to "easy".
    Its late over here , im going to try that tommorow.

    TY again.
    No sign

IMN logo majestic logo threadwatch logo seochat tools logo