TITLE: private typedefs used in public member functions (Source: comp.lang.c++.moderated, 28 Mar 2001) THOMAS-CRAMER: "Andrew R. Thomas-Cramer" > > Is it legal to do a private typedef, and use the synonym in public > > method declarations? That would prevent use of the typedef synonym by > > code external to the class, but allow simpler method declarations. > > (I've tried this in VC++, and it works -- but is it legal?) MENZL: "Gerhard Menzl" > Good question. I would think 11/5 allows the conclusion that it is > legal. Public methods are members, hence they may access other, private > members. But why do you want to do this? Muddling the public interface > with private aliases is confusing for clients, who can't use them > anyway. A major problem I have with C++ is that it is very hard to keep > the public interface free from implementation details, yet you seem to > want to drive this even further than the language requires. If clients > are supposed to use public names, these names should be used in the > public interface. You can still use the private shortcuts in the > implementation. NARAN: "Siemel Naran" Strange, but legal. Auto_ptr uses it. Recall, it is something like class auto_ptr { class auto_ptr_ref; public: auto_ptr(auto_ptr&); auto_ptr(auto_ptr_ref); operator auto_ptr_ref(); }; It's purpose is to enable users to call a function that returns an auto_ptr and save that auto_ptr directly in another auto_ptr. Basically, auto_ptr a(f()); The system will convert the auto_ptr returned by f() to an auto_ptr_ref through the operator conversion (for non-const member functions can be called on temporaries), then put the auto_ptr_ref into 'a' through the constructor. The direct call to auto_ptr's copy constructor does not work because it is of the form T::T(T& that) and thus requires a named temporary for 'that'. Though I think it is strange. One could have easily done auto_ptr a(f().release()); Besides, most times one will put the auto_ptr into another type of container, like a shared_ptr or even a raw pointer. So we'll use .release() anyway. And we wouldn't need the strange situation where a public function returns a private type. _______________________________________________ cpptips mailing list http://cpptips.hyperformix.com