Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ polymorphism with references
    primarykey
    data
    text
    <p>I've been writing some classes in C++ that operate on files. When I started, I made all my constructors accept one <code>std::string</code> argument that was interpreted as a filename, because that was quick and easy.</p> <p>The only functions specific <code>std::fstream</code> that any of the classes use is <code>.open()</code>. I figured that since <code>std::ofstream</code> and <code>std::ostringstream</code> both derive from <code>std::ostream</code> and I was only using <code>std::ostream</code>'s functions, that it would be much better to have each constructor take an <code>std::ostream</code> or <code>std::istream</code> argument as appropriate, instead of keeping the classes bound to files.</p> <p>A great textbook example of why polymorphism is useful. GCC is pretty unhappy about it though. This is a simplification of my code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; class Foo { public: Foo(std::istream&amp;); std::istream&amp; input; }; Foo::Foo(std::istream&amp; is) { this-&gt;input = is; } int main() { std::ifstream is("test.txt"); Foo foo(is); } </code></pre> <p>Which, when compiled with <code>g++ test.cpp</code>, produces the following colorful assortment of errors:</p> <pre><code>test.cpp: In constructor ‘Foo::Foo(std::istream&amp;)’: test.cpp:10:1: error: uninitialized reference member ‘Foo::input’ [-fpermissive] In file included from /usr/include/c++/4.7/ios:43:0, from /usr/include/c++/4.7/ostream:40, from /usr/include/c++/4.7/iostream:40, from test.cpp:1: /usr/include/c++/4.7/bits/ios_base.h: In member function ‘std::basic_ios&lt;char&gt;&amp; std::basic_ios&lt;char&gt;::operator=(const std::basic_ios&lt;char&gt;&amp;)’: /usr/include/c++/4.7/bits/ios_base.h:791:5: error: ‘std::ios_base&amp; std::ios_base::operator=(const std::ios_base&amp;)’ is private In file included from /usr/include/c++/4.7/ios:45:0, from /usr/include/c++/4.7/ostream:40, from /usr/include/c++/4.7/iostream:40, from test.cpp:1: /usr/include/c++/4.7/bits/basic_ios.h:64:11: error: within this context In file included from /usr/include/c++/4.7/iostream:41:0, from test.cpp:1: /usr/include/c++/4.7/istream: In member function ‘std::basic_istream&lt;char&gt;&amp; std::basic_istream&lt;char&gt;::operator=(const std::basic_istream&lt;char&gt;&amp;)’: /usr/include/c++/4.7/istream:56:11: note: synthesized method ‘std::basic_ios&lt;char&gt;&amp; std::basic_ios&lt;char&gt;::operator=(const std::basic_ios&lt;char&gt;&amp;)’ first required here test.cpp: In constructor ‘Foo::Foo(std::istream&amp;)’: test.cpp:11:16: note: synthesized method ‘std::basic_istream&lt;char&gt;&amp; std::basic_istream&lt;char&gt;::operator=(const std::basic_istream&lt;char&gt;&amp;)’ first required here </code></pre> <p>I have trouble sifting through all the template arguments but it looks like it doesn't like <code>this-&gt;input = is;</code>. It's important for each class to be able to access the stream as a member, because various different member functions all have to be able to see it. I figured GCC would probably be nicer about a pointer, but to me, pointers to classes look suspiciously like dynamically allocated memory, and I only want to raise those suspicions if they're true. What's the right way to go about this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload