🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by "Douglas C. Schmidt" Mon, 21 Nov 1988 05:58
Strange g++ feature/bug
#1534
Author: "Douglas C. Schmidt"
Date: Mon, 21 Nov 1988 05:58
56 lines
2050 bytes
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 ] ) );
}
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads