One more thing to add ...

Program 1 fails
# include <ostream>

using namespace std;

class thing : private ios_base {
   ostream& xo;
public:
   thing(ostream& y) : xo(y) { xo = y; }
};

gcc.3.4.4 messaging
x.cpp: In member function `std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char, std::char_traits<char> >::operator=(const std::basic_ios<char, std::char_traits<char> >&)': /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ios_base.h:784: error: `std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private
x.cpp:9: error: within this context

std::ios_base was never meant to be copy-able/assign-able, this has nothing to do with public/private *inheritance*, since it is the members of the base that are private, and thus inaccessible to any derived classes.

In your thing::thing ctor:

"xo(y)" initializes the member *reference* (essentially taking the address of y), whereas "xo = y;" is assigning the *object* referenced by 'ox', which is not the same. This is why you hit the inaccessible assignment error.

The real problem is that you are assigning an ostream to an ostream, which is not allowed *in any context* because ostream ultimately derives from ios_base, which privatizes assignment. You are seeing this message about ios_base (misleading you to think it has to do with your thing class inheriting from ios_base) because the default assignment of class ostream (not explicitly defined) is invoking the default assignments of its base classes, including ios_base. This is more an issue of mis-using the standard ostream class.

Fang

David Fang
http://www.csl.cornell.edu/~fang/
http://www.achronix.com/

Reply via email to