Re: Templates and const function name resolution
Greg Herlihy wrote:
kanze wrote:
Dimitar wrote:
I have the following class C and the function F:
class C
{
public:
const vector<int>& GetV() const { return m_v; }
private:
vector<int>& GetV() { return m_v; }
vector<int> m_v;
};
template<class T1, class T2>
void F(const typename vector<T1>::iterator& first,
const typename vector<T1>::iterator& last)
{
for (vector<T1>::iterator it = first; it != last; ++it)
{
const vector<T2>& v = it->GetV();
}
}
...
I am wondering why the code as presented above does not
compile and how can I make it compile without introducing
const iterator?
Change the name of one of the functions. It is almost
always a bad idea for functions with the same name to have
different access rights.
I would go further and question whether declaring a private,
accessor method makes much sense in the first place. After
all, any context in which the private GetV() method could be
called is also a context that that enjoys direct access to the
private m_v member variable. And if C's implemention cannot
handle its own m_v data member according to its own
specifications, then there's little chance that any other code
in the program will do any better.
In a simple case like the above, I would agree. On the other
hand, I've used private accessors for objects that were lazily
constructed, and I could see there use any time there was a
chance that the function do more than just return the immediate
object.
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]