home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / std / cplus / 2143 < prev    next >
Encoding:
Text File  |  1993-01-28  |  2.2 KB  |  65 lines

  1. Xref: sparky comp.std.c++:2143 comp.lang.c++:20089
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!swrinde!cs.utexas.edu!sun-barr!news2me.EBay.Sun.COM!seven-up.East.Sun.COM!sungy!news.Sweden.Sun.COM!swippy!seunet!sunic!mcsun!sun4nl!ruuinf!prisma.cv.ruu.nl!rvloon
  3. From: rvloon@cv.ruu.nl (Ronald van Loon)
  4. Newsgroups: comp.std.c++,comp.lang.c++
  5. Subject: Pointer to functions and const-ness
  6. Message-ID: <1993Jan27.143102.20008@cv.ruu.nl>
  7. Date: 27 Jan 93 14:31:02 GMT
  8. Sender: usenet@cv.ruu.nl (Usenet Network)
  9. Organization: University of Utrecht, 3D Computer Vision Research Group
  10. Lines: 51
  11. Originator: rvloon@triton.cv.ruu.nl
  12. Nntp-Posting-Host: triton.cv.ruu.nl
  13.  
  14. A question to all C++ gurus out there:
  15.  
  16. Consider the following class definition:
  17.  
  18. class test
  19. {
  20.   private:
  21.     int iPrivateInteger;
  22.  
  23.   public:
  24.     virtual void f(void (*)(int&));
  25.     virtual void f(void (*)(const int&)) const;
  26.  
  27.     virtual void g();
  28.     virtual void h() const;
  29. };
  30.  
  31. The purpose of the two definitions of f (note the placing of const) is to pass
  32. the integer 'iPrivateInteger' to the function that is passed as an argument.
  33.  
  34. Obviously, when a const object of this class is used, one does not want the
  35. internal integer to change, hence the definition of the prototype is different
  36. as well. 
  37.  
  38. Say I have two functions:
  39.  
  40. void non_const_function(int&)
  41. {
  42. }
  43.  
  44. void const_function(const int&)
  45. {
  46. }
  47.  
  48. The first one matches the prototype of the first 'f', the second one of the
  49. second 'f'. So, if I pass 'const_function' to an object of this class, the
  50. second 'f' should be called, and conversely if I pass 'non_const_function' the
  51. first 'f' should be called.
  52.  
  53. The question is: is that correct ? Intuitively I would say yes, but maybe
  54. function-parameters do not take 'const' into account. (in effect :
  55.  
  56. void (*)(int&) is equivalent to void (*)(const int&)); another possibility is
  57. that void(*)(const int&) can be implicitly converted to void (*)(int&).
  58.  
  59. Any opinions ?
  60. -- 
  61. Ronald van Loon     | Consider this: In the United States, an automobile is
  62. (rvloon@cv.ruu.nl)  | stolen EVERY 14.7 SECONDS. 
  63. 3DCV Group, Utrecht |   If that statistic scares you, think how we felt when we 
  64. The Netherlands     | made it up. - Dave Barry, "CHRISTMAS BUYERS GUIDE"
  65.