home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!think.com!sdd.hp.com!zaphod.mps.ohio-state.edu!not-for-mail
- From: ren@function.mps.ohio-state.edu (Liming Ren)
- Newsgroups: comp.lang.c++
- Subject: Automatic conversion! Can we do this?
- Date: 22 Dec 1992 15:11:52 -0500
- Organization: Department of Mathematics, The Ohio State University
- Lines: 39
- Distribution: world
- Message-ID: <1h7sq8INNr7e@function.mps.ohio-state.edu>
- NNTP-Posting-Host: function.mps.ohio-state.edu
-
- I wrote a string class and overloaded operator + to achive the function
- of strcat(). It is my understanding that the compiler will do the conversion
- from char * to string using the function I supplied. What is strange is I can
- do:
-
- string a, b("Merry");
- a=b+" Chrismas";
- a="C" + b;
-
- But I can't do: a="Merry" + " Chrismas";
- The compiler complains that "pointer +pointer" and the conversion does not
- happen. I tried to overload + with (char*, char*). The compiler did not
- allow me. HERE is the class:
-
- class string{
- char *str;
- int size;
- public:
- string() {str=new char[10]; size=9;}
- string(const char *p){ str=strdup(p); size=strlen(p);}
- string(const string& s){str=strdup(s.str); size=s.size;}
- ~string(){delete [] str;}
- string& operator=(const string&s){/*body*/ }
- friend string operator+(const string&,const string&){/*body*/}
- };
-
- main()
- {
- string a;
- a="Merry" +" Chrismas";
- }
-
- It seems to me the compiler has no clue what is going on if I supply two
- pointers. It needs at least one operand to be string to invoke the
- overloaded + on string class. Is there a way to overcome this?
-
-
- Merry Christmas!
-
-