1

I have a class as follows and it has a member of istringstream type. In the constructor for the class what can the istringstream type extracted_text be initialized to?

class user_input
{
std::string input_text;
std::istringstream extracted_text;

public:
user_input()
{
input_text="NULL";
// want to know what to initialize extracted_text with here
}
}

1 Answers1

2
// want to know what to initialize extracted_text with here

You cannot do that here. Class members are initialized before the constructors body is executed. If you want to initialize members you need to do that before the contructor runs.

what can the istringstream type extracted_text be initialized to?

Here you can find a list of constructors, pick one: std::basic_istringstream::basic_istringstream. For example you can initialize it with a std::string, (3) on the list:

Uses a copy of str as initial contents of the underlying string device. The underlying basic_stringbuf object is constructed as basic_stringbuf<Char,Traits,Allocator>(str, mode | std::ios_base::in).

class user_input
{
    std::string input_text;
    std::istringstream extracted_text;

    public:
    user_input() : input_text("NULL"),extracted_text("hello world")
                // ^^ member initializer list
    {
    }
};

What do istringstreams or stringstreams in general initialize to by default in C++?

The default constructor (same link):

Default constructor. Constructs new underlying string device with the default open mode

If that is what you want you do not have to provide an initializer.

Read more on constructors and the member initializer list here. I didn't mention default member initializers about which you can read here.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • This helps a lot. im not that advanced a to follow that lingo in the link https://en.cppreference.com/w/cpp/io/basic_istringstream/basic_istringstream but i will try to learn it. for now `extracted_text("")` helps. Thank you! – Pratap Biswakarma Sep 05 '20 at 00:55
  • 1
    @PratapBiswakarma it can be confusing at first. Just remember that all those `basic_foo`s are nothing you have to care about if you don't want to. All you need to remember is that eg `std::string` is a shorthand for `std::basic_string` and similar for the others – 463035818_is_not_an_ai Sep 05 '20 at 00:58