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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!gatech!darwin.sura.net!spool.mu.edu!agate!linus!philabs!acheron!scifi!watson!yktnews!admin!wo0z!lwloen
  3. From: lwloen@rchland.vnet.ibm.com (Larry Loen)
  4. Subject: Re: Automatic conversion! Can we do this?
  5. Sender: news@rchland.ibm.com
  6. Message-ID: <1992Dec28.193617.7436@rchland.ibm.com>
  7. Date: Mon, 28 Dec 1992 19:36:17 GMT
  8. Reply-To: lwloen@rchland.vnet.ibm.com
  9. Disclaimer: This posting represents the poster's views, not necessarily those of IBM
  10. References:  <1h7sq8INNr7e@function.mps.ohio-state.edu>
  11. Nntp-Posting-Host: wo0z.rchland.ibm.com
  12. Organization: IBM Rochester
  13. Lines: 53
  14.  
  15. Won't the following work:
  16.  
  17. a = string("Merry") + "Christmas"   ;
  18.  
  19. By converting "Merry" to a string, it should enable your overloaded +
  20. operator to be found.  It works on my C++ compiler:
  21.  
  22.  /*  Non-production code!!  */
  23.  
  24. #include <iostream.h>
  25. #include <string.h>
  26.  class string{
  27.     char *str;
  28.     int size;
  29.     friend ostream& operator<< (ostream& x,string& y)
  30.           { x << y.str; return x; };
  31.    public:
  32.      string()  {str=new char[10]; size=9;}
  33.      string(const char *p){ str=strdup(p); size=strlen(p);}
  34.      string(const string& s){str=strdup(s.str); size=s.size;}
  35.      ~string(){delete [] str;}
  36.      string& operator=(const string& s){ strcpy(str,s.str); return *this; }
  37.      friend string operator+(const string& x,const string& y)
  38.            { char* str2;
  39.              str2= new char[strlen(x.str)+strlen(y.str)+1]; strcpy(str2,x.str);
  40.              strcpy(str2+strlen(x.str),y.str); return string(str2); };
  41.   };
  42.  
  43. int main()
  44.  
  45. {
  46.      string a;  string hny;
  47.  
  48.  
  49.      /******  the point of it all is this next line *******/
  50.        a= string("Merry") +" Christmas";
  51.        cout << a;
  52.      /******   and, a secondary point *****/
  53.        hny = string(" and ") + "a " + "Happy New " + "Year! ";                            
  54.        cout << hny;                                                                       
  55.        return 0;
  56. }
  57.  
  58.  
  59. Not 100% of what you are looking for, to be sure, but at least one avoids
  60. declaring "b".  I believe you will find that only the first such reference
  61. needs to be a string as my second example shows.  You might even try
  62. string("") as an experiment and see what happens.  (E.g string("")+ "Merry "
  63. + "Christmas").
  64.  
  65. -- 
  66.    Larry W. Loen        |  My Opinions are decidedly my own, so please
  67.                         |  do not attribute them to my employer
  68.