Hi,
as all C++ books I've read so far tell me that const_casts are something really evil, I am trying to avoid them everywhere I can. However I came up with an issue where I don't really know whether avoiding them is worth the extra work.
I am writing a class for a tree which stores elements of class Leaf. A function search exists with several 100 lines of implementation. The interface looks like:
Code:
class Leaf;
class LeafAttributes;
class Tree
{
public:
[...]
Leaf& search( const LeafAttributes& );
const Leaf& search( const LeafAttributes& ) const;
}
Now of course I don't want to really implement search twice, as the algorithm does not differ for the const and non-const versions. So what I'm doing at the moment is this:
Code:
class Leaf;
class LeafAttributes;
class Tree
{
public:
[...]
Leaf& search( const LeafAttributes& a )
{ return const_cast<Leaf&> _search( a ); }
const Leaf& search( const LeafAttributes& ) const
{ return _search( a ); }
private:
const Leaf& _search( const LeafAttributes& ) const
}
So the question I'm asking: Is there a nice, clean way to implent this without using the const_cast?
Thanks,
Torsten