I’m wondering about one thing. I’ve got class which has 1 overloaded member function:
class A{
public:
class const_iterator{};
class iterator : public const_iterator{};
iterator find(const K &key);
const_iterator find(const K &key) const;
};
Ad. iterator is inheriting from const_iterator, but it isn’t adding anything.
What I want to do is, inside normal find call const find. Something like this:
typename A::iterator A::find(const K &key){
const_iterator it(find(key));
return (*(iterator*)&it);
}
I don’t need different implementation of non-const find ATM. Is it possible to do something like this? Because now I’m getting into infinite loop, adding “A::” before find isn’t changing anything.
In general, there’s no clean solution for this, unfortunately.
You can call the overloaded
findby simply castingthistoA const*– but the result will be of the wrong type (const_iteratorrather thaniterator) and there may not be a conversion between these in the general case (your(*(iterator*)&it)won’t work in your case).But of course, in your special case, since you defined the two classes, you can define such a conversion by adding an appropriate constructor to
iterator:Then you can re-write your non-
constfindimplementation as follows:Incidentally, note the absence of
typenamein the return type. SinceA::iteratorisn’t a dependent name, you don’t need (and, at least in C++03, are not allowed to) usetypenamehere.