C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 31st, 2003, 05:04 PM
tony825 tony825 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Greece
Posts: 63 tony825 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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
}
__________________
No sign

Last edited by tony825 : January 31st, 2003 at 05:07 PM.

Reply With Quote
  #2  
Old February 1st, 2003, 01:48 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,595 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 32 m 17 sec
Reputation Power: 1009
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.

Reply With Quote
  #3  
Old February 1st, 2003, 05:25 PM
tony825 tony825 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Greece
Posts: 63 tony825 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Which Child class will be poped out of the stack?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT