This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: operator=() issue
- From: Arthur Schwarz <aschwarz1309 at verizon dot net>
- To: David Fang <fang at csl dot cornell dot edu>
- Cc: gcc at gcc dot gnu dot org
- Date: Fri, 10 Apr 2009 19:40:23 -0700 (PDT)
- Subject: Re: operator=() issue
To all, I stand abashed - don't try this without a trained instructor. I misread the Schildt quote and (I think) wasted your time(s).
Thank you
art
--- On Fri, 4/10/09, David Fang <fang@csl.cornell.edu> wrote:
> From: David Fang <fang@csl.cornell.edu>
> Subject: Re: operator=() issue
> To: "Arthur Schwarz" <aschwarz1309@verizon.net>
> Cc: gcc@gcc.gnu.org
> Date: Friday, April 10, 2009, 5:45 PM
> 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/
>
>