schmidt%glacier.ics.uci.edu@ORION.CF.UCI.EDU ("Douglas C. Schmidt") (11/21/88)
Hi,
I'm not sure if the following code works due to any intentional G++
design feature, but it is certainly interesting! I seem to be able to
create anonymous recursively defined structures, use casts to manage
dynamically created linked lists, and not have to worry about
introducing a conflicting type name into the global namespace. This
appears to solve a particularly nasty defect of C and C++, i.e.,
inability to hide type names inside of classes ( the Ada folks always
snicker at this flaw, unfortunately ).
Naturally, the AT&T translator chewed this up and spit it back out,
so I suspect it is *not* a planned and portable feature for G++?!
Doug
----------------------------------------
#include <stream.h>
class Linked_List {
private:
struct { // No name for struct
int i;
struct *link; // Anonymous recursive link field!
} *Head, *Temp;
public:
Linked_List ( int Size ) { // warning: completely brain-damaged code ahead!
Head = ( void * ) new char [ sizeof ( *Head ) ];
Head->i = 0;
Temp = Head;
for ( int i = 1; i < Size; i++ ) {
Temp->link = ( void * ) new char [ sizeof ( *Head ) ];
Temp = ( void * ) Temp->link;
Temp->i = i;
}
Temp->link = 0;
}
~Linked_List ( void ) { // Print out the contents of the anonymous list!
for ( Temp = ( void * ) Head; Temp; Temp = ( void * ) Temp->link ) {
cout << Temp->i << "\n";
}
}
};
main ( int, char *argv [ ] ) {
Linked_List List ( atoi ( argv [ 1 ] ) );
}