Thread: How to initialize an ostrstream object in a user defined class

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    How to initialize an ostrstream object in a user defined class

    How can we declare an oststream object in our own user defined class??

    I tried this but it didn't work..

    Code:
    class myclass
    {
    char str[80];
    std::ostrstream(str,80);
    };
    Last edited by juice; 02-29-2012 at 06:32 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Don't use ostrstream, use std::ostringstream.

    Next, why do you want a stringstream as a member of the class?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by laserlight View Post
    why do you want a stringstream as a member of the class?
    Just curious!! I was trying to derive my own class from ostrstream class such that it also contains a function for overloaded >> operator, but I couldn't declare an object of type ostrstream in my class. Is it possible??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by juice
    I was trying to derive my own class from ostrstream class
    The ostrstream class is deprecated. Is there any special reason why you want to use it?

    Quote Originally Posted by juice
    such that it also contains a function for overloaded >> operator, but I couldn't declare an object of type ostrstream in my class. Is it possible?
    Yes. You should declare the object as a member:
    Code:
    class myclass
    {
        char str[80];
        std::ostrstream;
    };
    Then initialise it in a constructor. Note that you are not deriving from ostrstream.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by laserlight View Post
    Yes. You should declare the object as a member:
    Code:
    class myclass
    {
        char str[80];
        std::ostrstream;
    };
    Then initialise it in a constructor. Note that you are not deriving from ostrstream.
    How can we initialize it in the constructor?? Isn't it supposed to be initialized right when it is declared??

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by juice
    How can we initialize it in the constructor?
    By using the constructor's initialiser list.

    Quote Originally Posted by juice
    Isn't it supposed to be initialized right when it is declared?
    No, because a class is a blueprint for objects of the class type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assertion: How to initialize a class object and use it
    By animation in forum C++ Programming
    Replies: 7
    Last Post: 03-26-2011, 04:55 AM
  2. STL Map Problem with user defined Class
    By noobcpp in forum C++ Programming
    Replies: 21
    Last Post: 07-21-2008, 12:02 PM
  3. class template user defined obj
    By terracota in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2004, 08:14 AM
  4. How do you initialize a template class object?
    By adonisv in forum C++ Programming
    Replies: 5
    Last Post: 02-12-2003, 06:38 PM
  5. Requirements for using a user-defined class in STL
    By NixPhoeni in forum C++ Programming
    Replies: 8
    Last Post: 10-21-2001, 02:41 PM