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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!news.univie.ac.at!scsing.switch.ch!univ-lyon1.fr!ghost.dsi.unimi.it!rpi!usenet.coe.montana.edu!saimiri.primate.wisc.edu!sdd.hp.com!spool.mu.edu!sgiblab!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: Friend ..Overload Assignment opertr ..
  5. Message-ID: <1993Jan27.142604.16923@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <1993Jan22.075317.21062@usl.edu> <C19838.JM3@cs.uiuc.edu>
  10. Date: Wed, 27 Jan 1993 14:26:04 GMT
  11. Lines: 42
  12.  
  13. In article <C19838.JM3@cs.uiuc.edu> pjl@cs.uiuc.edu (Paul Lucas) writes:
  14. >In <1993Jan22.075317.21062@usl.edu> rks9954@usl.edu (Srinivasa Rao K) writes:
  15. >
  16. >>       I am writing a "string"  class which i should be able
  17. >>       to use like
  18. >
  19. >>       string str1;
  20. >>       str1 = "Hello world" ;
  21. >
  22. >>       But We canot use a friend to overload the assignment oprator.
  23. >>      So finally  I had to convince myself to use something like
  24. >
  25. >>       str1 <= "Hello world"
  26. >
  27. >>       which works.
  28. >
  29. >>      Is there any way to do the first method ....
  30. >
  31. >    No.  operator=() must be a member.  Period.  Everybody defines it
  32. >    this way and has no problem.  Whats wrong with it being a member?
  33.  
  34.     Actually, I suggest instead a constructor:
  35.  
  36.     string::string(char *);
  37.  
  38. Then you dont need to bother with assignment from a char*:
  39.  
  40.     string s;
  41.     s="Hello";
  42.  
  43. will work because the assignment operator:
  44.  
  45.     operator=(const string&)
  46.  
  47. will cause conversion from char* to string before doing the assignment.
  48. This is less efficient but simpler.
  49.  
  50. -- 
  51. ;----------------------------------------------------------------------
  52.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  53.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  54. ;------ SCIENTIFIC AND ENGINEERING SOFTWARE ---ph:  2 799 8223 --------
  55.