template <class T> class CObject
{
public:
operator float ( )
{
return (float)m_Data;
}
T m_Data;
};
void main ( )
{
CObject<int> O;
float F;
O.m_Data = 43;
F = O; // This calls operator float()
}
Conversion operators are called automatically when a type is tried to be converted into another type. Saying F = O converts the CObject<int> O into a float by calling the operator.
By the way, don''t worry about the apparent lack of purpose in making O a CObject<int> and then converting it to float... I just do that as an example.
Now, what I want to do is be able to write a conversion operator for any type, by taking advantage of templates. My reason for this is very complicated and I won''t go into it, but I wrote the conversion operator looking like this:
template <class T> class CObject
{
public:
template <class C> operator C ( )
{
return (C)m_Data;
}
T m_Data;
};
That seems to compile just fine, but I can''t figure out the correct syntax for calling such an operator. So what would be the correct replacement for "F = O" to work with that operator?
BTW, another acceptable operator would be this:
template <class T> class CObject
{
public:
template <class C> C operator () ( )
{
return (float)m_Data;
}
T m_Data;
};
The non-template version of that operator would be called like:
F = O();
But I don''t know about the template version. Any ideas?
~CGameProgrammer( );