Discussion:
How to override a MFC macro template
(too old to reply)
Alexh
2009-09-23 21:44:44 UTC
Permalink
Hi,

I have created my own SerializeElements() function but when I run the
code it still uses MFC's version.

I have the following line in my .h file -

class MyClass : Cobject
{
....
};

void AFXAPI SerializeElements(CArchive& ar,CChartInfo* pChartInfo, int
nCount);


and the following in my .cpp file (at the end)-

void AFXAPI SerializeElements(CArchive& ar, CChartInfo* pChartInfo,
int nCount)
{
for(int i=0;nCount;i++,pChartInfo++)
{
pChartInfo->Serialize(ar);
}
}

But the code MyCList.Serialize(ar) still ends up calling the
SerializeElements in afxtempl.h.

Whats the rule for overriding functions of this type? I do not have
#include <afxtempl.h> in my files but I know it's being compiled by
other code in my project.

Thanks
Igor Tandetnik
2009-09-23 22:09:22 UTC
Permalink
Post by Alexh
I have created my own SerializeElements() function but when I run the
code it still uses MFC's version.
I have the following line in my .h file -
class MyClass : Cobject
{
....
};
void AFXAPI SerializeElements(CArchive& ar,CChartInfo* pChartInfo, int
nCount);
I believe you need to specialize SerializeElements template for your
type, rather than overloading it with a non-template function. Like
this:

template<>
void AFXAPI SerializeElements<CCharInfo>(CArchive& ar,CChartInfo*
pChartInfo, int
nCount);

The code in CList::Serialize calls SerializeElements with explicit
template parameter list, like this:

SerializeElements<TYPE>(ar, pData, 1);

This call will never pick a non-template overload.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Ben Voigt [C++ MVP]
2009-09-24 14:28:52 UTC
Permalink
Post by Igor Tandetnik
Post by Alexh
I have created my own SerializeElements() function but when I run the
code it still uses MFC's version.
I have the following line in my .h file -
class MyClass : Cobject
{
....
};
void AFXAPI SerializeElements(CArchive& ar,CChartInfo* pChartInfo,
int nCount);
I believe you need to specialize SerializeElements template for your
type, rather than overloading it with a non-template function. Like
template<>
void AFXAPI SerializeElements<CCharInfo>(CArchive& ar,CChartInfo*
pChartInfo, int
nCount);
The code in CList::Serialize calls SerializeElements with explicit
SerializeElements<TYPE>(ar, pData, 1);
This call will never pick a non-template overload.
Also be careful that the specialization is in scope every time
CList<CChartInfo> is used. The compiler won't generate calls to functions
it doesn't know about.

Loading...