Re: "trivial" problem with template method pattern
rogo wrote:
Ok, when I began to implement it, I thought it should be
straightforward and easy. I'm not sure whats wrong with the following
code:
class A
{
public:
A() {}
int get()
{
int a;
get(a);
return a;
}
protected:
virtual void get(int&) = 0;
};
class B : public A
{
public:
B(int a) : b(a) {}
using A::get();
private:
void get(int& a)
{
a = b;
}
int b;
};
int main() {
B b(1);
b.get();// thats a problem, and I dont know why
}
For some reason I dont understand yet the compiler can't "find" the
inherited methode "int get()". I use g++ version 4.1.2 20070925.
Behavior is correct. The declaration of B::get(int&) hides all other
instances of get() for B. You have two choices: put a using declaration
in all your child classes (see above), or rename get(int&). The second
is a better solution, especially since get(int&) is private.