Hi, everyone.
The following program is an approach to use C++ streams with TCP sockets.
(I know there are free libraries that do that, but...)
The current implementation depends on the fact, that a member of my stream
class will be initialized first and sent to the base class constructor later.
(See tcp_istream). However, it does not work and I wonder if this is something
I cannot do with C++ in general. The compiler error message tells me that
the base class constructor is called before the initialization of the argument,
and that's exactly what I want to avoid. Is it possible to influence, in which
order the constructor initializer list's entries are evaluated? Or is it a bad
thing to depend on this?
Thanks.
By the way: I'm using GCC 3.3.1, but MS-VC++ refuses to do this too.
Here's the code and the compiler output:
Code:
#include <iostream>
struct tcp_socket {
// do some low-level network stuff
};
struct tcp_streambuf : std::streambuf {
tcp_streambuf(tcp_socket &t) : sock(t) {}
private:
tcp_socket &sock;
};
struct tcp_istream : std::istream {
tcp_istream(tcp_socket &sock) : s(sock), std::istream(s) {} // <-- FAILS!
private:
tcp_streambuf s;
};
int main() {
tcp_socket a;
tcp_istream b(a);
}
Code:
1.cc: In constructor `tcp_istream::tcp_istream(tcp_socket&)':
1.cc:17: warning: `tcp_istream::s' will be initialized after
1.cc:14: warning: base `std::basic_istream<char, std::char_traits<char> >'
1.cc:14: error: no matching function for call to `std::basic_istream<char,
std::char_traits<char> >::basic_istream(const <anonymous>**, tcp_streambuf&)
'
/usr/include/g++/iosfwd:61: error: candidates are: std::basic_istream<char,
std::char_traits<char> >::basic_istream(const std::basic_istream<char,
std::char_traits<char> >&)
/usr/include/g++/istream:105: error: std::basic_istream<_CharT,
_Traits>::basic_istream(std::basic_streambuf<_CharT, _Traits>*) [with _CharT
= char, _Traits = std::char_traits<char>]