templates & inheritance := problems

 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//--BaseAndChild.h
#pragma once
class Base
{
public:
Base(){}
virtual ~Base(){}
template<typename T> char* foo(T something);
};
template<typename T> char* Base::foo(T something)
{
return "generalized base";
}
template<> char* Base::foo<int>(int something)
{
return "specialized base";
}
class Child : public Base
{
public:
Child(){}
~Child(){}
template<typename T> char* foo(T something);
};
template<typename T> char* Child::foo(T something)
{
return "generalized child";
}
template<> char* Base::foo<char>(char something)
{
return "specialized child";
}
//--main.cpp
#include <BaseAndChild.h>
void main
{
Base base;
Child child;
char c('0');
int i(0);
cout<<base.foo(base)<<"\n"; //OK
cout<<base.foo(i)<<"\n"; //OK
cout<<child.foo(base)<<"\n"; //OK
cout<<child.foo(c)<<"\n"; //FAIL
cin>>c;
}