Calling method of specialized template base class in sub-class
TheKamis (2)
Feb 10, 2013 at 3:26pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
class IFoo
{
virtual void Bar() = 0;
};
class FooAbstract
{
virtual void Bar() {}
};
template <class T>
class FooTemplate
{
public :
virtual void Bar();
T _param;
};
template <class T>
FooTemplate<T>::Bar()
{
cout << "FooTemplate<T>::Bar() " << _param << endl;
}
class FooDerived : public FooTemplate<int >
{
public :
virtual void Bar();
// additional methods
};
FooDerived::Bar()
{
cout << "FooDerived::Bar() " << _param << endl;
this ->FooTemplate<int >::template Bar<int >(); // error
}
How to call the Bar() method from FooTemplate in FooDerived::Bar() ?
JLBorges (1754)
Feb 10, 2013 at 3:39pm UTC
1 2 3 4 5 6 7 8 9 10 11
template < typename T > struct B { template < typename U > void bar() {} } ;
template < typename T > struct D : B<T>
{
template < typename U > void bar()
{
// ...
B<T>::template bar<U>() ;
// ...
}
};
TheKamis (2)
Feb 10, 2013 at 3:46pm UTC
Unfortunately, this is not the same case. In my case the base of FooDerived is a specialized template class.
In fact I made a mistake in the description code. In real case I did not overwrite the Base() method in FooTemplate. As a result FooDerived couldn't call Bar() from its base. And the base in that case was FooAbstract::Base(), which was unaccessible. Why?
When I overwrote Base in FooTemplate the code started to work fine. But still, even if I didn't overwrite the Base() method in FooTemplate it should be called from the closest base of a base of a base... that was implemented. But wasn't...
Last edited on Feb 10, 2013 at 3:58pm UTC
JLBorges (1754)
Feb 10, 2013 at 3:56pm UTC
What difference does that make?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
template < typename T > struct B { template < typename U > void bar() {} } ;
template <> struct B<int > { template < typename U > void bar() ; } ;
struct D : B<int >
{
template < typename U > void bar()
{
// ...
B<int >::bar<U>() ;
B<int >::template bar<U>() ;
// ...
}
};
template < typename U > void B<int >::bar() {}
vlad from moscow (3654)
Feb 10, 2013 at 4:01pm UTC
1 2 3 4 5 6 7 8 9 10
template <class T>
struct A
{
virtual void f() { std::cout << "A<T>::f()\n" ; }
};
struct B : A<int >
{
void f() { A<int >::f(); std::cout << "B::f()\n" ; }
};
AbstractionAnon (517)
Feb 10, 2013 at 4:04pm UTC
Line 33 is missing a void type.
Simply qualify the call to Bar by the base class (what you used on line 25):
1 2 3 4
void FooDerived::Bar()
{ cout << "FooDerived::Bar() " << _param << endl;
FooTemplate<int >::Bar();
}