Martin B. wrote:
DeMarcus wrote:
Hi,
I have some trouble understanding the true meaning of const.
Please consider this example.
(....)
Now, this gives me "invalid conversion from 'const int*' to 'int*'" when
exposing item_ in store.add( &item_ ). What's the way of thinking here?
(....)
4. Redesign according to C++ Coding Standards by Sutter & Alexandrescu,
Item 11 - Hide information. Basically it says; "Don't expose internal
information from an entity that provides an abstraction.". However, my
real problem (illustrated with Item) is a kind of wrapper similar to
boost::shared_ptr and I need something like boost::shared_ptr::get().
Therefore I'm clueless about finding an alternative design.
A const member function must not only not modify the object, it must
also not expose anything that could be used to directly modify the
object.
(....)
Thanks all of you for great input! Here's a more detailed version of my
structure.
class NumberCruncher // Previously Store, and it may change values
{
public:
template<typename T>
void addItem( T* item ) { /* Do number crunching on item */ }
};
class ItemContainerInterface // Previously just Item
{
public:
// This is the const in question.
virtual addMe( NumberCruncher& ) const = 0;
virtual removeMe( NumberCruncher& ) const = 0;
};
template<typename T>
class ItemContainer : public ItemContainerInterface
{
public:
virtual addMe( NumberCruncher& nc ) const
{ nc.addItem( &item_ ); }
virtual removeMe( NumberCruncher& nc ) const
{ nc.removeItem( &item_ ); }
private:
T item_;
};
template<typename T>
class ItemSharedPtrContainer : public ItemContainerInterface
{
public:
virtual addMe( NumberCruncher& nc ) const
{ nc.addItem( item_.get() ); }
virtual removeMe( NumberCruncher& nc ) const
{ nc.removeItem( &item_.get() ); }
private:
boost::shared_ptr<T> item_;
};
(....)
I've left out some functions here and there but the general message is
that the previous Store (now NumberCruncher) can alter the data in the
pointer it gets. Not directly with addItem() but at a later time.
The problem is that the item container can hold both shared pointers and
the type itself. So sometimes it's an ItemContainer and sometimes it's
an ItemValue. In both cases it is a kind of owner of the value, so maybe
I have to remove const from ItemContainerInterface::addMe() because the
value may be changed at a later time by NumberCruncher.
The difficulty is that ItemSharedPtrContainer only holds a pointer and
therefore addMe() could be const. But ItemContainer holds the value and
therefore it cannot be const. Shall I just remove const to support both
containers or is there a way to refactor the design?
(vector etc.). These use const memberfunctions to return const
references and non-const member function to give access to modifyable items.
call addMe() on a _const_ ItemContainerInterface. I'm not sure this
would make sense, so dropping the const might be a good idea.
member of ItemContainer mutable.
[ comp.lang.c++.moderated. First time posters: Do this! ]