K
kuangye
Hi, all.
///////////////////////////////////
////////code sample
/////compiling error in vs2005 and gcc3.4
///////////////////////////////////
class CB;
CB fn(CB& obj);
class CB
{
protected:
CB(){}
CB(const CB& ob){}
friend CB fn(CB& obj);
};
CB fn(CB& obj)
{
return CB();
}
//////////////////////////////////////
template<typename T>
class TA;
template<typename T>
TA<int> toolfn(TA<T>& obj);
template<typename T>
class TAublic CB
{
template<typename U>
friend class TA;
public:
TA(){};
protected:
//should be protected or private
TA(const CB& ob):CB(ob){}
public:
friend TA<int> toolfn<T>(TA<T>& obj);
};
template<typename T>
TA<int> toolfn(TA<T>& obj)
{
return TA<int>( fn(obj) );
}
int main()
{
TA<float> f1;
TA<int> ret = toolfn(f1);//----->how to make it pass the compiling.
/*
compiling error:
1.cpp: In function `TA<int> toolfn(TA<T>&) [with T = float]':
1.cpp:52: instantiated from here
1.cpp:36: error: `TA<T>::TA(const CB&) [with T = int]' is protected
1.cpp:46: error: within this context
*/
return 0;
}
////////////////////////////////////////////
///currently, i can using a helper function to walk around this
//but it seems that it does not elegant enough...
///////////////////////////////
Any hint will be appreciated
///////////////////////////////////
////////code sample
/////compiling error in vs2005 and gcc3.4
///////////////////////////////////
class CB;
CB fn(CB& obj);
class CB
{
protected:
CB(){}
CB(const CB& ob){}
friend CB fn(CB& obj);
};
CB fn(CB& obj)
{
return CB();
}
//////////////////////////////////////
template<typename T>
class TA;
template<typename T>
TA<int> toolfn(TA<T>& obj);
template<typename T>
class TAublic CB
{
template<typename U>
friend class TA;
public:
TA(){};
protected:
//should be protected or private
TA(const CB& ob):CB(ob){}
public:
friend TA<int> toolfn<T>(TA<T>& obj);
};
template<typename T>
TA<int> toolfn(TA<T>& obj)
{
return TA<int>( fn(obj) );
}
int main()
{
TA<float> f1;
TA<int> ret = toolfn(f1);//----->how to make it pass the compiling.
/*
compiling error:
1.cpp: In function `TA<int> toolfn(TA<T>&) [with T = float]':
1.cpp:52: instantiated from here
1.cpp:36: error: `TA<T>::TA(const CB&) [with T = int]' is protected
1.cpp:46: error: within this context
*/
return 0;
}
////////////////////////////////////////////
///currently, i can using a helper function to walk around this
//but it seems that it does not elegant enough...
///////////////////////////////
Any hint will be appreciated