Skip to main content
gamedev.net
🔒 Locked

Question on Nested Types

Started by Big Sassy May 25, 2002 at 1:43 PM 1 replies 200+ views
Original Post
Big Sassy
Big Sassy
How do you declare one? How do you use one? What the hell are they for?! I''m reading about STL and I see that it can be used to define the type name of the data inside an iterator and things like that. But I''m still very confused. Could someone give me the lowdown on Nested Types and show me an example of code? I''d really appreciate it. ***********************           
Grib
Grib
It is a way to hide some implementation details from the outside world. If your class needs a member that has complex behiviour that is highly specific to that class then nested classes are the way to go. The cannonical example is any class that manages a large linked data structure. Individual nodes are frequently implemented as a nested class.


  
template < class Type >
class BadStack {
struct node {
Type data;
node *next;
node(const Type &d, next *n) : data(d), next(n) {}
};
node *head,*tail;
public:
BadStack() : head(0), tail(0) {}
void push(const Type &d) {
head = new node(d,head);
if(tail==0) tail=head;
}
...
};


In the above we don''t want users of our class to play with nodes, in particular the consructor requires that we always pass a pointer to the first node in a list. At the same time if for some reason we did want users to play with nodes, but still would freqently mannage them from another class then nesting the class acts as a strong reminder that the two are linked. It would be relitivly trivial to change node into a forward itterator, then add begin() and end() members to BadStack. If we later decided that calling new/delete was more trouble than it was worth, and thus wanted to use a dynamic array then we only have one place to change node into a somewhat glorified int.
Big Sassy
Big Sassy
ahhhhhh. Makes sense. Thanks a lot man! I thought nobody was going to answer this post after a while.

***********************
          

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.