String + int (overloading)

This is a discussion on String + int (overloading) within the C++ Programming forums, part of the General Programming Boards category; Hi! I want to overload operator +, so I can write strings in this way => String s = "Age: ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question String + int (overloading)

    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)

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    28
    Here is some code that may be educational:
    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");
    Don't ask me why. Maybe it will help you, though.
    Last edited by Orborde; 05-10-2005 at 11:40 PM. Reason: bold relevant bits

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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.

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    All I want to do is this =>
    Code:
    int cal = 200
    String s = "Result is " + calc;
    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.

    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)

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    38
    Better create a general numeric-to-string conversion template, then catenate the strings with standard operator+.

  6. #6
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    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)

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    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?

  8. #8
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    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)

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A very brief search on Google tuend up this which should be rather helpful.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by GaPe
    Ok this works! Thanks.

    But what about overloading this =>

    int cal = 200;
    char *text = "Result is " + calc;

    How can I overload this?
    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);'
    Yet you can do

    Code:
    int cal = 200;
    std::string text = "Result is ";
    text = text + calc;
    //or
    text += calc;
    I use MS VC++ 6, and std::string + int operator is ambigous - buggy libs. So have overloaded it. First you try something...
    If this helps: print the number to a string, then concatnate it with the other string.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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;
    }
    Funny thing about:
    Code:
    int cal = 200;
    char *text = "Result is " + calc;
    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 touching

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21