home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progc / fixed300.arj / BUG169.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-14  |  983 b   |  45 lines

  1. #if 0
  2. From: Bhatia
  3. Subject: Virtual destructor bug
  4. Status: Not a compiler bug. Needs X::X(X&) and operator=(X&) defined.
  5. #endif
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. class Company
  12. {
  13.     char *name;
  14. public:
  15.     Company(char *s) {name=new char[strlen(s+1)]; strcpy(name,s);}
  16.     virtual ~Company() { printf("\nC destructor"); delete name;}
  17.     void org_name() { printf("%s",name);}
  18. };
  19.  
  20. class Division : public Company
  21. {
  22.     char *manager;
  23. public:
  24.     Division(char *s,char *mgr) : Company(s) {manager=new char[strlen(mgr+1)]; strcpy(manager,mgr);}
  25.     ~Division() {printf("\nD destructor"); delete manager;}
  26. };
  27.  
  28. void main()
  29. {
  30.     Company *companies[3];
  31.     companies[0] = new Company("Bilbo Sofware, Inc.");
  32.     companies[1] = new Division("Vert Apps", "Ron Herold");
  33.     companies[3] = new Division("Horiz Apps", "Bob Young");
  34.     for( int i=0;i<3;i++)
  35.         delete companies[i];
  36. };
  37.  
  38. // Compiled with 2.18
  39. // Output from the above file
  40.  
  41. //C Destructor
  42. //
  43. //Heap is corrupted
  44.  
  45.