How to initialize an ostrstream object in a user defined class

This is a discussion on How to initialize an ostrstream object in a user defined class within the C++ Programming forums, part of the General Programming Boards category; How can we declare an oststream object in our own user defined class?? I tried this but it didn't work.. ...

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

    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 05:32 AM.

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

    Next, why do you want a stringstream as a member of the class?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05: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

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