
Hi, now I have a new problem - const correctness. ---8<--- #include <iostream> #include <algorithm> #include <iterator> #include <vector> #include <boost/shared_ptr.hpp> class Doc { public: virtual ~Doc() { } public: const std::vector<double>& foo() const { return m_foo; } protected: std::vector<double>& foo() { return m_foo; } private: std::vector<double> m_foo; }; using namespace std; int main() { boost::shared_ptr<Doc> lp_doc( new Doc ); std::copy(lp_doc->foo().begin(), lp_doc->foo().end(), std::ostream_iterator<double>(cout, " ") ); cout << endl; } --->8--- `std::vector<double, std::allocator<double> >& Doc::foo()' is protected I hoped std::copy takes the const foo() member function. Thanks olaf

On Tuesday, August 27, 2002, at 09:23 AM, Olaf Petzold wrote:
int main() { boost::shared_ptr<Doc> lp_doc( new Doc );
std::copy(lp_doc->foo().begin(), lp_doc->foo().end(), std::ostream_iterator<double>(cout, " ") ); cout << endl; }
`std::vector<double, std::allocator<double> >& Doc::foo()' is protected
This is not a shared_ptr problem. The same thing would happen if you were using Doc *. You could fix it by using Doc const *, and in the shared_ptr case you can fix it by using boost::shared_ptr<Doc const>. Or you could fix the problem by using different names for the const and non-const foo() functions. Your original design shows some confusion about C++ rules about access control. Making something protected does not affect whether it's considered when doing overload resolution, so it's impractical to have a pair of const/not-const functions overloaded the way you do. -- Darin

int main() { boost::shared_ptr<Doc> lp_doc( new Doc );
std::copy(lp_doc->foo().begin(), lp_doc->foo().end(), std::ostream_iterator<double>(cout, " ") ); cout << endl; }
`std::vector<double, std::allocator<double> >& Doc::foo()' is protected
This is not a shared_ptr problem. The same thing would happen if you were using Doc *. You could fix it by using Doc const *, and in the shared_ptr case you can fix it by using boost::shared_ptr<Doc const>.
Or you could fix the problem by using different names for the const and non-const foo() functions. Your original design shows some confusion about C++ rules about access control. Making something protected does not affect whether it's considered when doing overload resolution, so it's impractical to have a pair of const/not-const functions overloaded the way you do.
Using protected did show that the non const member was taken - a "furtune" for me of testing the public interface. Only the derived classes should use protected ( I did hide that part from snippet). I hoped, that std::copy takes the const iterators, after looking into the headers - it takes generell iterators. Thanks Olaf
participants (2)
-
Darin Adler
-
Olaf Petzold