home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18388 < prev    next >
Encoding:
Internet Message Format  |  1992-12-24  |  3.2 KB

  1. Path: sparky!uunet!pipex!bnr.co.uk!uknet!fulcrum!bham!bhamvx!mccauleyba
  2. From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Sizeof and Virtual functions
  5. Message-ID: <1992Dec24.132121.1@vax1.bham.ac.uk>
  6. Date: 24 Dec 92 13:21:21 GMT
  7. References: <1992Dec24.061254.9763@nuscc.nus.sg>
  8. Organization: University of Birmingham
  9. Lines: 53
  10. NNTP-Posting-Host: vax1.bham.ac.uk
  11.  
  12. In article <1992Dec24.061254.9763@nuscc.nus.sg>, suresh@papaya.iss.nus.sg (Suresh Thennarangam - Research Scholar) writes:
  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. This is done to facilitate late binding 
  17. > at run-time depending on the type of the object being pointed to
  18. > rather than the type of the object pointer as opposed to normal
  19. > member functions calls which are bound at compile time. 
  20. > Is this an implementation-specific issue only ?  
  21. Ish.
  22.  
  23. > A C++ programmer should  not have to know of these ugly little pointers
  24. > lurking around in his structures. These monsters add 4 bytes each
  25. > (on a 32 bit machine) to your structure; sizeof no longer tells the 
  26. > truth and seemingly innocent  actions like fwrite'ing such structures 
  27. > to  a file can quickly assume rather pernicious connotations. 
  28. > If I write classes with virtual functions to interpret and save 
  29. > file formats like XWD, TIFF, GIF , etc and innocently use sizeof() 
  30. > to calculate the size and write to a file I will be writing along 
  31. > addresses in my machines data area. Obviously, no one will be able
  32. > to read those files either. Of course an experienced C++ programmer
  33. > would know how work around this problem  but this just an illustration. 
  34. > Isn't  there a better way to implement virtual functions ? 
  35. No. The only other way would be to include the type information in the pointer
  36. itself so all pointers to objects of classes with virtual functions would
  37. become bigger. This would also mean that if a pointer were converted to void*
  38. and back it would loose it's type information. (I'm not sure if a compiler
  39. with this property would be conforming but it would certainly be unpopular).
  40.  
  41. Another solution would be to adjust the action of the `sizeof' and `(void*)'
  42. operators to refer to the object excluding its type indicator but
  43. this would break any code that used `malloc'.
  44.  
  45. > Isn't the current implementation a clever hack at best ?
  46. Possibly but the only alternative would be a clumsy hack.
  47.  
  48. It is better to have `sizeof' and interconversion to `void*' do what
  49. they do now rather than try to hide the type information outside of the
  50. object.
  51.  
  52. Almost any use of void* and sizeof is implementation dependant, they are
  53. really part of C and C++ programmers should avoid them whenever possible.  
  54. -- 
  55.     \\   ( )    No Bullshit!     |  Email: B.A.McCauley@bham.ac.uk   
  56.  .  _\\__[oo        from         |  Voice: +44 21 471 3789 (home)
  57. .__/  \\ /\@    /~)  /~[   /\/[  |    Fax: +44 21 625 2175 (work)
  58. .  l___\\      /~~) /~~[  /   [  |  Snail: 197 Harborne Lane,
  59.  # ll  l\\    ~~~~ ~   ~ ~    ~  |         Birmingham, B29 6SS, UK
  60. ###LL  LL\\   (Brian McCauley)   |   ICBM: 52.5N 1.9W
  61.