Initializing base class in unusual way

Hi, I know that we can initialize base class like this Derived::Derived(int x) : Base(x){} but what if my constructor of derived class takes std::istream& as argument and only after reading information about base classes object can initialize it?

for example
1
2
3
4
5
6
7
8
9
struct Base{
    Base(string);
    //...
};

struct Derived : Base{
    Derived(istream&);
    //...
}

If I want to get a single string from istream and give it to Base how could I write this constructor of Derived?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct base { explicit base( std::string ) ; /* ... */ };

struct derived : base
{
    explicit derived( std::istream& stm )
        : base( [&stm] { std::string s ; stm >> s ; return s ; }() ) {}

    // ...
};

struct derived2 : base
{
    static std::string str_from_stream( std::istream& stm ) { std::string s ; stm >> s ; return s ; }

    explicit derived2( std::istream& stm ) : base( str_from_stream(stm) ) {}

    // ...
};
This is so cool man, I'm actually feeling bad that I didn't figure this out myself =[ But anyway God bless u man!
Topic archived. No new replies allowed.