constructor's initializer list: order of evaluation

This is a discussion on constructor's initializer list: order of evaluation within the C++ Programming forums, part of the General Programming Boards category; Hi, everyone. The following program is an approach to use C++ streams with TCP sockets. (I know there are free ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    62

    constructor's initializer list: order of evaluation

    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>]

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    The std::istream (typedef for a basic_istream for char) expects a pointer to the buffer and not an object. Therefore:
    Code:
    tcp_istream(tcp_socket &sock) : s(sock), std::istream(&s) {} // <-- WORKS!
    Is it possible to influence, in which
    order the constructor initializer list's entries are evaluated?
    Yes, variables in the initializer list are initiated in the order they were declared in the class. However I am pretty sure base::classes will alway get initialized before any member of a derived class
    Last edited by Darryl; 05-10-2005 at 07:15 PM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    Yes, now it works! Thank you, Darryl.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-28-2008, 12:24 AM
  3. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 07:16 PM
  4. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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