 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Leon Wu Guest
|
Posted: Mon Nov 13, 2006 10:10 am Post subject: question about Partial Template Specialization |
|
|
I have some code like following:
template<class T>
class A
{
public:
A(T& t) : m_b(t){};
void Func()
{
m_b.DoSome();
};
private:
B<t> m_b;
};
---------------------------------------
template<class T>
class B
{
public:
void DoSome() {
cout << "type t";
};
private:
T &t;
};
template<>
void B<int>::DoSome()
{
cout << "type int";
}
---------------------------------------
main()
{
A<int> a;
a.Func();
}
the output is "type t" instead of "type int", why?! any help is
appericiated. |
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Mon Nov 13, 2006 10:10 am Post subject: Re: question about Partial Template Specialization |
|
|
Leon Wu wrote:
| Quote: | I have some code like following:
template<class T
class A
{
public:
A(T& t) : m_b(t){};
void Func()
{
m_b.DoSome();
};
private:
B<t> m_b;
};
---------------------------------------
template<class T
class B
{
public:
void DoSome() {
cout << "type t";
};
private:
T &t;
};
template
void B<int>::DoSome()
{
cout << "type int";
}
---------------------------------------
main()
{
A<int> a;
a.Func();
}
the output is "type t" instead of "type int", why?! any help is
appericiated.
|
Please post a complete compilable program...
After fixing a number of compile errors you get this -
this says "type int"
#include <iostream>
template<class T>
class B;
template<class T>
class A
{
public:
A() {};
A(T& t) : m_b(t){};
void Func()
{
m_b.DoSome();
};
private:
B<T> m_b;
};
//---------------------------------------
template<class T>
class B
{
public:
void DoSome() {
std::cout << "type t";
};
private:
T &t;
};
template<>
void B<int>::DoSome()
{
std::cout << "type int";
}
//---------------------------------------
int main()
{
A<int> a;
a.Func();
} |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|