home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18513 < prev    next >
Encoding:
Text File  |  1992-12-29  |  3.2 KB  |  72 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Sizeof and Virtual functions
  5. Message-ID: <1992Dec29.211505.21322@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <1992Dec24.061254.9763@nuscc.nus.sg>
  8. Date: Tue, 29 Dec 1992 21:15:05 GMT
  9. Lines: 61
  10.  
  11. suresh@papaya.iss.nus.sg (Suresh Thennarangam - Research Scholar) writes:
  12.  
  13. >One of the things that distinguishes virtual functions from other
  14. >normal member functions of a class is that some compilers that I
  15. >know of, store an extra pointer that is  probably used to index 
  16. >a vector table...
  17.  
  18. >Is this an implementation-specific issue only ?  
  19.  
  20. Yes.  A pointer to an array of pointers to the virtual member functions
  21. of a class is the usual implementation method.  It is not required by
  22. the language, but is easy to implement and efficient at run time.
  23.  
  24. >A C++ programmer should  not have to know of these ugly little pointers
  25. >lurking around in his structures.
  26.  
  27. Right, but see below.
  28.  
  29. >These monsters add 4 bytes each
  30. >(on a 32 bit machine) to your structure; sizeof no longer tells the 
  31. >truth and seemingly innocent  actions like fwrite'ing such structures 
  32. >to  a file can quickly assume rather pernicious connotations. 
  33.  
  34. 'sizeof' better tell the truth.  It must report the amount of storage
  35. required by an object of that type.  If the object has extra data added
  36. by the compiler, that is part of its size.  This is conceptually no
  37. different from the padding a compiler might add to get suitable alignment
  38. of individual members.  You are not allowed to assume the size of any
  39. type other than "char" (which is always 1 by definition).  In C as well
  40. as C++, struct types might have a size different from that calculated
  41. by adding the size of each member.
  42.  
  43. >If I write classes with virtual functions to interpret and save 
  44. >file formats like XWD, TIFF, GIF , etc and innocently use sizeof() 
  45. >to calculate the size and write to a file I will be writing along 
  46. >addresses in my machines data area. Obviously, no one will be able
  47. >to read those files either. Of course an experienced C++ programmer
  48. >would know how work around this problem  but this just an illustration. 
  49.  
  50. You are ignoring a critical part of C++.  Objects of class type must be
  51. constructed.  Reading data from a file into raw memory is not in general
  52. the same as constructing an object.  The exact layout of members of a
  53. class is not promised in general either, so blindly overwriting a class
  54. object is likely to produce poor results in general.
  55.  
  56. If you have an object of class type which has no base classes, no
  57. virtual member functions, no members of pointer type, and no static data
  58. members, you should be able to get away with a bit-copy to external
  59. storage and bit-copy back.
  60.  
  61. A C++ class is an abstraction mechanism.  This means you give up 
  62. intimate control of the representation in exchange for the ability to
  63. deal with object using a portable, high-level approach.
  64.  
  65. C++ allows control of pure data types in exactly the same way as C.
  66. You can, for example, create a C-style struct containing just the data
  67. of interest, and derive a class from it which has all the functionality
  68. you want.  You can still write and read back the data as you would in C.
  69. -- 
  70.  
  71. Steve Clamage, TauMetric Corp, steve@taumet.com
  72.