I'm trying to make a member template function, declared in a non-template base class, a friend of the derived class. Both methods of declaring it work using Visual Studio 2012. However, Visual Studio 2003 complains with errors below. As a workaround I'm declaring the base class as a friend, but I'd like to avoid that. Is this a limitation of VS2003 or is there another way?
template<class T> friend CBase* CBase::Make() const;
and
friend CBase* CBase::Make<CChild>() const;
Context:
#include "stdafx.h"
#include <string.h>
#include <iostream>
class CBase
{
public:
virtual CBase* MakeSameObj() = 0;
const std::string& Get() const { return data; }
CBase() { data += __FUNCSIG__; }
protected:
template<class T>
CBase* Make() const
{
CBase* pt = new T(__FUNCSIG__);
return pt;
}
std::string data;
};
class CChild
: public CBase
{
public:
CChild() : CBase() { data += " "; data += __FUNCSIG__; }
private:
CChild(char* param) : CBase() { data += " "; data += param; }
CBase* MakeSameObj() { return Make<CChild>(); }
//error C2245: non-existent member function 'Make' specified as friend (member function signature does not match any overload)
template<class T> friend CBase* CBase::Make() const;
//error C2768: 'CBase::Make' : illegal use of explicit template arguments
friend CBase* CBase::Make<CChild>() const;
//Ok
};
int _tmain(int argc, _TCHAR* argv[])
{
CBase* pt = new CChild;
CBase* pt1 = pt->MakeSameObj();
std::cout << pt->Get().c_str() << std::endl;
std::cout << pt1->Get().c_str() << std::endl;
return 0;
}