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

  1. Path: sparky!uunet!think.com!sdd.hp.com!zaphod.mps.ohio-state.edu!not-for-mail
  2. From: ren@function.mps.ohio-state.edu (Liming Ren)
  3. Newsgroups: comp.lang.c++
  4. Subject: Automatic conversion! Can we do this?
  5. Date: 22 Dec 1992 15:11:52 -0500
  6. Organization: Department of Mathematics, The Ohio State University
  7. Lines: 39
  8. Distribution: world
  9. Message-ID: <1h7sq8INNr7e@function.mps.ohio-state.edu>
  10. NNTP-Posting-Host: function.mps.ohio-state.edu
  11.  
  12. I wrote a string class and overloaded  operator + to achive the function
  13. of strcat(). It is my understanding that the compiler will do the conversion
  14. from char * to string using the function I supplied. What is strange is I can
  15. do:
  16.  
  17. string a, b("Merry");
  18. a=b+" Chrismas";
  19. a="C" + b;
  20.  
  21. But I can't do:   a="Merry" + " Chrismas";
  22. The compiler complains that "pointer +pointer" and the conversion does not 
  23. happen. I tried to overload + with (char*, char*). The compiler did not 
  24. allow me. HERE is the class:
  25.  
  26. class string{
  27.   char *str;
  28.   int size;
  29.  public:
  30.   string()  {str=new char[10]; size=9;}
  31.   string(const char *p){ str=strdup(p); size=strlen(p);}
  32.   string(const string& s){str=strdup(s.str); size=s.size;}
  33.   ~string(){delete [] str;}
  34.   string& operator=(const string&s){/*body*/    }
  35.   friend string operator+(const string&,const string&){/*body*/}
  36. };
  37.  
  38. main()
  39. {
  40.   string a;
  41.   a="Merry" +" Chrismas";
  42. }
  43.  
  44. It seems to me the compiler has no clue what is going on if I supply two
  45. pointers. It needs at least one operand to be string to invoke the
  46. overloaded + on string class. Is there a way to overcome this?
  47.  
  48.  
  49. Merry Christmas!
  50.  
  51.