Thread: tostring()

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    5

    tostring()

    is there a toString() method in c just like in C#?

    v=k.toString();

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    First ask yourself if there are classes in C. Then when you realize there aren't, ask yourself why the heck you asked such a silly question in the first place.

    No strings in C (outside of char arrays), no classes, no OOP (unless you simulate it with function pointers, etc.).... What are you even trying to do and why are you asking this?

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    5
    chill... typo...its c++...i know there arent any classes in c

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Typo? Mmkay, then this should be in the C++ section.

    And no, there is no standard toString() function/method in C++. It's usually done via operator overloading.... ie. << operator for std::cout.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Moved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    try std::stringstream

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jackel7777 View Post
    is there a toString() method in c just like in C#?

    v=k.toString();
    Templates templates templates.

    Code:
    template <typename T>
    std::string toString(const T &val_p)
    {
        std::stringstream x;
        x << val_p;
        return x.str();  
    }
    This function will convert ANY type of object to a string, provided that the appropriate ostream insertion operator is defined. To convert custom objects, you merely write a function:

    Code:
    std::ostream &operator<<(std::ostream &stream_p, const my_class &val_p)
    {
        // ...
        return stream_p;
    }
    This function will automatically be used by the toString() function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ problem with Shape hierarchy ~
    By jackfraust in forum C++ Programming
    Replies: 56
    Last Post: 04-14-2009, 11:13 AM
  2. Trying to print toString from object held in vector
    By whiskedaway in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2009, 06:13 AM
  3. toString() const string&
    By noobcpp in forum C++ Programming
    Replies: 3
    Last Post: 07-22-2008, 03:17 AM
  4. Can someone help with my seg fault?
    By John_L in forum C++ Programming
    Replies: 23
    Last Post: 03-01-2008, 04:04 PM
  5. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM