Hi!
I want to overload operator +, so I can write strings in this way => String s = "Age: " + 20; // int, long and double
How am I supposed to do that?
Hi!
I want to overload operator +, so I can write strings in this way => String s = "Age: " + 20; // int, long and double
How am I supposed to do that?
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Here is some code that may be educational:
Don't ask me why. Maybe it will help you, though.Code:class thing { thing(char * str) { /* do stuff*/ }; }; struct memptr { void * operator =(void * val) { value = val; return value; }; }; /*...later...*/ // memptr obj = new thing("HELLO"); // the above line does not work; the two lines below do memptr obj; obj = new thing("HELLO");
Last edited by Orborde; 05-10-2005 at 11:40 PM. Reason: bold relevant bits
String operator+(int) is already overloaded...
don't do math with strings
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
All I want to do is this =>
And this code must produce a string which looks like this=> s = "Result is 200". I want to create java-like String class. That's it.Code:int cal = 200 String s = "Result is " + calc;
So if anyone has any good suggestion how can I add numbers to string than please tell me, otherwise don't reply at all.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Better create a general numeric-to-string conversion template, then catenate the strings with standard operator+.
I don't want to use templates. I want to overload.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
This shows how to convert a number to a string the rest you should be able to figure out.
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
Woop?
Ok this works! Thanks.
But what about overloading this =>
int cal = 200;
char *text = "Result is " + calc;
How can I overload this?
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
You can't overload that example because at least one parameter from a operator function must be a class. You have 2 primitive types (char* and int), it doens't work. If you could, someone would have fun overloading 'int operator+(int,int);'Originally Posted by GaPe
Yet you can do
I use MS VC++ 6, and std::string + int operator is ambigous - buggy libs. So have overloaded it. First you try something...Code:int cal = 200; std::string text = "Result is "; text = text + calc; //or text += calc;
If this helps: print the number to a string, then concatnate it with the other string.
Funny thing about:Code:#include <iostream> #include <string> #include <sstream> std::string operator+ (const std::string& s, int i) { std::ostringstream os; os << i; return s + os.str(); } int main() { using namespace std; string s = string("The result is: ") + 200; cout<<s<<endl; }
is that "Result is " is really just a const char* so by adding calc to it you are getting some memory you probably shouldn't be touchingCode:int cal = 200; char *text = "Result is " + calc;