This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: operator=() issue


operator=() is private in ios_base. Using private inheritance of ios_base the program below fails in the constructor when '=' is used (but not during memory initialization). I don't understand why assignment is prohibited.

art

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.

HTH,

Fang


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]