home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!gatech!darwin.sura.net!spool.mu.edu!agate!linus!philabs!acheron!scifi!watson!yktnews!admin!wo0z!lwloen
- From: lwloen@rchland.vnet.ibm.com (Larry Loen)
- Subject: Re: Automatic conversion! Can we do this?
- Sender: news@rchland.ibm.com
- Message-ID: <1992Dec28.193617.7436@rchland.ibm.com>
- Date: Mon, 28 Dec 1992 19:36:17 GMT
- Reply-To: lwloen@rchland.vnet.ibm.com
- Disclaimer: This posting represents the poster's views, not necessarily those of IBM
- References: <1h7sq8INNr7e@function.mps.ohio-state.edu>
- Nntp-Posting-Host: wo0z.rchland.ibm.com
- Organization: IBM Rochester
- Lines: 53
-
- Won't the following work:
-
- a = string("Merry") + "Christmas" ;
-
- By converting "Merry" to a string, it should enable your overloaded +
- operator to be found. It works on my C++ compiler:
-
- /* Non-production code!! */
-
- #include <iostream.h>
- #include <string.h>
- class string{
- char *str;
- int size;
- friend ostream& operator<< (ostream& x,string& y)
- { x << y.str; return x; };
- 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){ strcpy(str,s.str); return *this; }
- friend string operator+(const string& x,const string& y)
- { char* str2;
- str2= new char[strlen(x.str)+strlen(y.str)+1]; strcpy(str2,x.str);
- strcpy(str2+strlen(x.str),y.str); return string(str2); };
- };
-
- int main()
-
- {
- string a; string hny;
-
-
- /****** the point of it all is this next line *******/
- a= string("Merry") +" Christmas";
- cout << a;
- /****** and, a secondary point *****/
- hny = string(" and ") + "a " + "Happy New " + "Year! ";
- cout << hny;
- return 0;
- }
-
-
- Not 100% of what you are looking for, to be sure, but at least one avoids
- declaring "b". I believe you will find that only the first such reference
- needs to be a string as my second example shows. You might even try
- string("") as an experiment and see what happens. (E.g string("")+ "Merry "
- + "Christmas").
-
- --
- Larry W. Loen | My Opinions are decidedly my own, so please
- | do not attribute them to my employer
-